去除空格:

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); 

 

相关文章:

  • 2021-08-15
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
  • 2021-08-27
  • 2022-12-23
  • 2021-08-15
猜你喜欢
  • 2021-07-03
  • 2022-12-23
  • 2021-12-08
  • 2022-12-23
  • 2021-06-14
  • 2022-12-23
  • 2021-11-05
相关资源
相似解决方案