去掉NSString中空格

NSString *s = @"this is    a  string    with lots  of     white space"; 
NSArray *comps = [s componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
 
NSMutableArray *words = [NSMutableArray array]; 
for(NSString *comp in comps) { 
  if([comp length] > )) { 
    [words addObject:comp]; 
  } 
} 
 
NSString *result = [words componentsJoinedByString:@" "]; 

 

你可能感兴趣的:(NSString)