可能的重复项:
如何删除 NSString 中多余的空白空间?
我正在尝试弄清楚如何从字符串中删除所有空格,但只留下一个。也就是说,如果我有一个像
"this is my test string"
结果将是
"this is my test string"
谢谢
更新:在这里找到答案删除 NS 中的多个空格
你可以试试这个:
NSString *string=@"this is my test string";
NSCharacterSet *spaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *tempArray = [[string componentsSeparatedByCharactersInSet:spaces] filteredArrayUsingPredicate:predicate];
string = [tempArray componentsJoinedByString:@" "];