开发者

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!


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜