问题
is there a simple way to remove the extra spaces in a string? ie like...
NSString *str = @"this string has extra empty spaces";
result should be:
NSString *str = @"this string has extra empty spaces";
Thanks!
回答1:
replace all double space with a single space until there are no more double spaces in your string.
- (NSString *)stripDoubleSpaceFrom:(NSString *)str {
while ([str rangeOfString:@" "].location != NSNotFound) {
str = [str stringByReplacingOccurrencesOfString:@" " withString:@" "];
}
return str;
}
来源:https://stackoverflow.com/questions/3870298/how-do-you-remove-extra-empty-space-in-nsstring