NSString 去除首尾和中间空格

转自 http://blog.csdn.net/zhaozy55555/article/details/7671499

//    NSString* trimedURL = [stringURL 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:@""]; 


你可能感兴趣的:(NSString 去除首尾和中间空格)