1.去掉两端空格:
[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ;
2.去掉所有空格:
[str stringByReplacingOccurrencesOfString:@" " withString:@""]
3.去掉多余空格:
- 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:@" "];
字符串去掉空格问题
原文:http://www.cnblogs.com/paideblogs/p/5150705.html