1.去掉两端的空格
- [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
2.去掉多余的空格
- NSString *str = @" this is a test . ";
- NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
- NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
- NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];
- NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
- str = [filteredArray componentsJoinedByString:@" "];
3.去掉所有空格
- [str stringByReplacingOccurrencesOfString:@" " withString:@""]
本文介绍了使用Objective-C进行字符串处理的三种方法:去除字符串两端的空白字符、去除字符串中的多余空白字符以及完全移除字符串中的空白字符。通过具体的代码示例展示了如何实现这些功能。
3076

被折叠的 条评论
为什么被折叠?



