您的位置:首页 > 移动开发 > IOS开发

NSString 去除空格方法 ios

2016-04-07 21:00 435 查看

1.去掉两端的空格

[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]   

2.去掉多余的空格

NSString *str = @"    this     is a    test    .   ";  

      

    NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];  

    NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];  

      

    NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];  

    NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];  

    str = [filteredArray componentsJoinedByString:@" "];  

3.去掉所有空格

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: