NSString去除两边空格字符,like trim()[转]

  1. NSString *cleanString = [dirtyString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];  

还有就是去除多于的空格,如下:

  1. NSString *theString = @"    Hello      this  is a   long       string!   ";  
  2.   
  3. NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];  
  4. NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];  
  5.   
  6. NSArray *parts = [theString componentsSeparatedByCharactersInSet:whitespaces];  
  7. NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];  
  8. theString = [filteredArray componentsJoinedByString:@" "];  
原文地址:https://www.cnblogs.com/zhkza99c/p/3794647.html