NSString去除两边空格字符,like trim()[转]
2014-06-18 15:11
246 查看
NSString *cleanString = [dirtyString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
还有就是去除多于的空格,如下:
NSString *theString = @" Hello this is a long string! ";
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [theString componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
theString = [filteredArray componentsJoinedByString:@" "];
还有就是去除多于的空格,如下:
NSString *theString = @" Hello this is a long string! ";
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [theString componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
theString = [filteredArray componentsJoinedByString:@" "];
相关文章推荐
- NSString去除两边空格字符,like trim()
- NSString去除两边空格字符,like trim()
- NSString去除空格字符-包括两边字符trim()
- NSString去除空格字符-包括两边字符trim()
- javascript 去除字符两边的空格函数(仿asp的trim)
- Javascript中去除两边的空格和JS中判断字符串中是否包含某字符
- Excel去除字符中的空格(trim)
- JavaScript去除字符串两边空格trim
- Java去除字符中的空格(trim)
- JavaScript去除字符串两边空格trim
- jQuery.trim() --去除字符两端空格
- c总结5 --- 去除两边空格(空白字符)(指针学习4)
- JavaScript trim()---去除字符串两边空格
- jQuery.trim() --去除字符两端空格
- trim 不仅能去除空格 还能去除以下字符
- JS中去除字符串两边的空格(trim(str))
- iOS NSString去除其他字符(空格回车符)
- OC-NSString去掉两边的空格,类似java中的trim
- Excel去除字符中的空格(trim)
- 用 Trim 函数去除字符串两边空白字符 和 用索引访问字符串