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:@" "];  

posted on 2014-06-18 15:11  zhkza99c  阅读(1125)  评论(0编辑  收藏  举报

导航