zoukankan      html  css  js  c++  java
  • 去空格 whitespaceAndNewlineCharacterSet和过滤字符串

     一、过滤字符串

      可以使用stringByTrimmingCharactersInSet函数过滤字符串中的特殊符号

      首先自己定义一个NSCharacterSet, 包含需要去除的特殊符号

    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_//|~<>$€^•'@#$%^&*()_+'/"""];
    
    
    
    由于NSString中有全角符号和半角符号, 因此有些符号要包括全角和半角的
    
    
    
    然后调用stringByTrimmingCharactersInSet
    
    
    
    NSString *trimmedString = [string stringByTrimmingCharactersInSet:set];
    
    
    
    trimmedString就是过滤后的字符串

    二、去除空格

      1.去掉两端的空格

      1 [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]   

      2.去掉多余的空格

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

      3.去掉所有空格

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

      4.去掉最左边的空格  和  去掉最右边的空格

    @interface NSString (TrimmingAdditions)  
    - (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet ;  
    - (NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet ;  
    @end  
      
    @implementation NSString (TrimmingAdditions)  
      
    - (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet {  
        NSUInteger location = 0;  
        NSUInteger length = [self length];  
        unichar charBuffer[length];      
        [self getCharacters:charBuffer];  
      
        for (location; location < length; location++) {  
            if (![characterSet characterIsMember:charBuffer[location]]) {  
                break;  
            }  
        }  
      
        return [self substringWithRange:NSMakeRange(location, length - location)];  
    }  
      
    - (NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet {  
        NSUInteger location = 0;  
        NSUInteger length = [self length];  
        unichar charBuffer[length];      
        [self getCharacters:charBuffer];  
      
        for (length; length > 0; length--) {  
            if (![characterSet characterIsMember:charBuffer[length - 1]]) {  
                break;  
            }  
        }  
      
        return [self substringWithRange:NSMakeRange(location, length - location)];  
    }  
      
    @end  

        例如:NSLog(@"%@",[@"abc 123 " stringByTrimmingRightCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]);

          :NSLog(@"%@",[@"0.012300" stringByTrimmingRightCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0"]]);

     一个非常好的例子,来源于http://nshipster.com/nscharacterset/, 去掉多余的空格(包括两端的和中间的)

    NSString *exampleStr = @" My name    is Johnny!";  
    exampleStr = [exampleStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];  
    NSArray *exampleArr = [exampleStr componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];  
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self <> ''"];  
    exampleArr = [exampleArr filteredArrayUsingPredicate:predicate];  
    exampleStr = [exampleArr componentsJoinedByString:@" "];
  • 相关阅读:
    网络管理和nmcli命令的使用——网络接口配置-bonding实验步骤
    raid组合优缺点介绍和创建LVM实验个人笔记
    磁盘分区就是这么简单,电脑小白都能看懂的磁盘分区教程!
    C盘优化之桌面移动法,拯救你爆满的C盘!
    电脑软件打开也有讲究,电脑软件打开方式总结!
    电脑使用建议大全,注意这些细节可以让你的电脑更好用!
    CentOS服务器apache绑定多个域名的方法
    CentOS 7使用yum安装PHP5.6
    PhpMyAdmin 配置文件现在需要一个短语密码的解决方法
    CentOs 7.*中配置安装phpMyAdmin的完整步骤记录
  • 原文地址:https://www.cnblogs.com/mkai/p/6193972.html
Copyright © 2011-2022 走看看