一、过滤字符串

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

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

NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_//|~<>$€^•'@#$%^&*()_+'/"""];

由于NSString中有全角符号和半角符号, 因此有些符号要包括全角和半角的

然后调用stringByTrimmingCharactersInSet

NSString *trimmedString = [string stringByTrimmingCharactersInSet:set];

trimmedString就是过滤后的字符串

二、去除空格

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

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

@interface NSString (TrimmingAdditions)
- (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet ;
- (NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet ;
@end @implementation NSString (TrimmingAdditions) - (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet {
NSUInteger location = ;
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 = ;
NSUInteger length = [self length];
unichar charBuffer[length];
[self getCharacters:charBuffer]; for (length; length > ; length--) {
if (![characterSet characterIsMember:charBuffer[length - ]]) {
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:@" "];

去空格 whitespaceAndNewlineCharacterSet和过滤字符串的更多相关文章

  1. 去空格 whitespaceAndNewlineCharacterSet

    http://blog.csdn.net/worn_nest/article/details/10155495

  2. linux makefile字符串操作函数 替换subst、模式替换patsubst、去首尾空格strip、查找字符串findstring、过滤filter、反过滤filter-out、排序函数sort、取单词word、取单词串wordlist、个数统计words

    1.1       字符操作函数使用 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函 ...

  3. javascript 过滤字符串中的中文与空格

    js 如何过滤字符串里中文或空格呢?方法有很多种,我们可以使用替换与正则表达式来实现,本文向大家介绍两个简单的例子,感兴趣的码农可以参考一下. 1.javascript过滤空格: function m ...

  4. javascript 字符串去空格

    1.正则去空格 a.去掉字符串中所有空格 " hello world ".replace(/\s+/g,"");//helloworld b.去掉字符串左边空格 ...

  5. DB中字段为null,为空,为空字符串,为空格要怎么过滤取出有效值

      比如要求取出微信绑定的,没有解绑的 未绑定,指定字段为null 绑定的,指定字段为某个字符串 解绑的,有的客户用的是更新指定字段为1,有的客户更新指定字段为‘1’ 脏数据的存在,比如该字段为空字符 ...

  6. 【SQL】字符串去空格解决方法

    一.表中字符串带空格的原因 1,空格就是空格. 2,控制符 显示为 空格. 二.解决方法 第一种情况,去空格的处理的比较简单,Replace(column,' ','') 就可以解决. 第二种情况,解 ...

  7. Foundation框架的一些实用方法:替换字符串,去空格,反转

    //定义一个可变字符串, Format后面可以跟字符串类型,也可以传入C语言的字符串数组 NSMutableString *str = [NSMutableString stringWithForma ...

  8. String字符串操作--切割,截取,替换,查找,比较,去空格.....

    字符串拼接 直接用+号:String a = "I"; String b = "love"; String c = "you";String ...

  9. ORACLE对字符串去空格处理(trim)

    首先便是这Trim函数.Trim 函数具有删除任意指定字符的功能,而去除字符串首尾空格则是trim函数被使用频率最高的一种.语法Trim ( string ) ,参数string:string类型,指 ...

随机推荐

  1. Junit4断言

    Junit4断言API: http://junit.org/javadoc/latest/index.html Constructor Summary protected Assert()       ...

  2. loaded the "ViewController" nib but the view outlet was not set.'

    错误代码: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[U ...

  3. 基于mini2440的uboot移植(一)

    一.移植环境 虚拟机:ubuntu12.04 uboot源码:u-boot-2008.10.tar.bz2 交叉编译:arm-linux-gcc-4.4.3 简单的记录下编译uboot的过程,要想具体 ...

  4. cs11_c++_lab7

    wcount.cc #include <iostream> #include <map> #include <string> #include <algori ...

  5. [转]常用的快速Web原型图设计工具

    转自大神: http://www.cnblogs.com/lhb25/archive/2009/04/25/1443254.html 做产品原型是非常重要的一个环节,做产品原型就会用使用各式各样的工具 ...

  6. tomcat 7/8 启动非常慢的解决方法

    在日志中发现启动慢的地方: 2016-11-14 09:31:30.522 [localhost-startStop-1] INFO o.s.c.s.DefaultLifecycleProcessor ...

  7. 9、SQL Server 操作数据

    插入数据 使用Insert Into 插入 if(exists(select * from sys.databases where name = 'webDB')) drop database web ...

  8. JavaBean,POJO,VO,DTO的区别和联系

    JavaBeans A JavaBean is a class that follows the JavaBeans conventions as defined by Sun. Wikipedia ...

  9. javascript之数组操作

    1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...

  10. 利用Sql实现将指定表数据导入到另一个数据库示例

    因为工作中经常需要将数据从一个数据库导入到另一个数据库中,所以将这个功能写成一个存储过程,以方便调用.现在粘贴出来供大家参考: 注意:1,以下示例中用到了syscolumns,sysobjects等系 ...