How do you remove extra empty space in NSString?

后端 未结 1 1429
遇见更好的自我
遇见更好的自我 2020-12-09 23:02

is there a simple way to remove the extra spaces in a string? ie like...

NSString *str = @\"this string has extra              empty spaces\";
相关标签:
1条回答
  • 2020-12-09 23:52

    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 讨论(0)
提交回复
热议问题