How do you remove extra empty space in NSString?
问题 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:@" "]; }