NSString去除空格字符-包括两边字符trim()

去除空格:

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

 

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

NSString *theString = @"    Hello      this  is a   long       string!   ";
    
    NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
    NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
    
    NSArray *parts = [theString componentsSeparatedByCharactersInSet:whitespaces];
    NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
    theString = [filteredArray componentsJoinedByString:@" "];
    NSLog(@"%@",theString); 

 

posted @ 2016-07-12 14:43  想吃天鹅肉的井底之蛙  阅读(1006)  评论(0编辑  收藏  举报