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:@" "];
时间: 2024-10-24 21:41:54