本文转载至 http://blog.csdn.net/samuelltk/article/details/8994313
1.去掉两端的空格
[cpp] view plaincopyprint?
- [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
2.去掉多余的空格
[cpp] view plaincopyprint?
- 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.去掉所有空格
[cpp] view plaincopyprint?
- [str stringByReplacingOccurrencesOfString:@" " withString:@""]
时间: 2024-10-02 06:33:51