美文网首页
iOS 去除空格

iOS 去除空格

作者: 彦子凡 | 来源:发表于2018-03-22 18:57 被阅读59次

前后空格

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

相关文章

网友评论

      本文标题:iOS 去除空格

      本文链接:https://www.haomeiwen.com/subject/frhzqftx.html