开发者

How do you remove extra empty space in NSString?

开发者 https://www.devze.com 2023-01-19 08:19 出处:网络
开发者_如何学运维is there a simple way to remove the extra spaces in a string?ie like... NSString *str = @\"this string has extraempty spaces\";
开发者_如何学运维

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

精彩评论

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

关注公众号