码迷,mamicode.com
首页 > 其他好文 > 详细

字符串去掉空格问题

时间:2016-01-22 13:47:06      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

1.去掉两端空格:

[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ;

2.去掉所有空格:

[str stringByReplacingOccurrencesOfString:@" " withString:@""]  

3.去掉多余空格:

  1. NSString *str = @"    this     is a    test    .   ";  
  2.       
  3.     NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];  
  4.     NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ‘‘"];  
  5.       
  6.     NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];  
  7.     NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];  
  8.     str = [filteredArray componentsJoinedByString:@" "];  

字符串去掉空格问题

标签:

原文地址:http://www.cnblogs.com/paideblogs/p/5150705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!