NSString去除空格字符-包括两边字符trim()
2014-03-15 14:18
411 查看
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去除空格字符-包括两边字符trim()
- NSString去除两边空格字符,like trim()
- NSString去除两边空格字符,like trim()[转]
- NSString去除两边空格字符,like trim()
- javascript 去除字符两边的空格函数(仿asp的trim)
- JavaScript去除字符串两边空格trim
- Excel去除字符中的空格(trim)
- iOS NSString去除其他字符(空格回车符)
- PHP中用正则表达式实现类似trim的功能(包括去除全角空格)
- c++ string 字符无法准确去除两边空格!
- Java去除字符中的空格(trim)
- Javascript中去除两边的空格和JS中判断字符串中是否包含某字符
- JavaScript去除字符串两边空格trim
- 编写函数去除字符串中包含的非字母字符(不包括空格)
- JS中去除字符串两边的空格(trim(str))
- JavaScript去除字符串两边空格 == trim
- jQuery.trim() --去除字符两端空格
- c总结5 --- 去除两边空格(空白字符)(指针学习4)
- iOS NSString去除其他字符(空格回车符)
- jQuery.trim() --去除字符两端空格