美文网首页
iOS 字符串 去除空格

iOS 字符串 去除空格

作者: 王_尧 | 来源:发表于2017-02-28 14:51 被阅读0次

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

    去掉两端的空格

    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    去掉多余的空格
    NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet]; NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"]; NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces]; NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings]; str = [filteredArray componentsJoinedByString:@" "];
    去掉所有空格

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

    相关文章

      网友评论

          本文标题:iOS 字符串 去除空格

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