• iOS NSString去除其他字符(空格回车符)


    很多时候大家都需要对字符串进行处理.现在就对字符串删掉特殊字符的处理进行总结一下.

    1.stringByTrimmingCharactersInSet  这个方法只能对字符串前后两个部分的特殊字符进行处理.而对于字符串中间的却无能为力了

         NSString *str =@"  123 ::daf 32  ";

        NSString * str2 = [strstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];//whitespaceAndNewlineCharacterSet去掉两端的空格和回车,其他一些参数可以自查

        NSLog(@"str2:%@",str2);

        NSString *str1 =@"12  e:e  ge : :: :::::";

        NSString *str3 = [str1stringByTrimmingCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@":"]];//去掉两端的冒号

        NSLog(@"str3:%@",str3);

        

     2.去除字符串中所有的特殊字符或者是不特殊的字符

     

    下面的代码是删除字符串中所有的空格,回车符:

     

        NSString *str = @"  123  ::daf 32  ";

    NSMutableString *mutStr = [NSMutableStringstringWithString:str];

        NSRange range = {0,str.length};

        [mutStr replaceOccurrencesOfString:@" "withString:@""options:NSLiteralSearchrange:range];

        NSRange range2 = {0,mutStr.length};

      [mutStr replaceOccurrencesOfString:@" "withString:@""options:NSLiteralSearchrange:range2];

        NSLog(@"%@",mutStr);

  • 相关阅读:
    Enum, Generic and Templates
    Writing A Threadpool in Rust
    A First Look at Rust Language
    Closures in OOC
    Complexity Behind Closure
    Introduction to OOC Programming Language
    OOC,泛型,糟糕的设计。
    Enlightenment笔记
    Machine Learning/Random Projection
    Machine Learning/Introducing Logistic Function
  • 原文地址:https://www.cnblogs.com/isItOk/p/6363710.html
Copyright © 2020-2023  润新知