有时候在后台下来的一串字符串,或者是上传的一串字符串中,我们不得不剔掉一些特殊字符.我们有几种解决方案.
下面以去除空格号来举例子
- 1.最简单的一种(思路上),便利字符串,有的地方使用NSRange截掉.
NSString *str = @"123412312312 1312 3 9827 892147 829472894 asdjsalkdjs n kjhbjk kh日 福气阿萨德爱好女偶尔会 接口和收到两封";//随便打的字符串
while ([str rangeOfString:@" "].length != 0) {//表明有这个字段
NSRange range = [str rangeOfString:@" "];
str = [str stringByReplacingCharactersInRange:range withString:@""];
}
NSLog(@"%@",str);
- 2.使用NSString的API替换字符
NSString *str = @"123 4 5 6 78 9 1 2 34 567891234 5 6 7 89 1 2 3 4 5 6 7 8912345 6789123456789";
str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"%@",str);
- 3.把字符串变成数组.然后把数组转回成字符串
NSString *str = @"1hasjdskah kgiusdfgh igs kjg khjfg sdkj hgoiurye skd nlshf 发给一分公司地分公司手工课返回改 ";
NSArray *strArray = [str componentsSeparatedByString:@" "];
NSLog(@"%@",strArray);
//这个如果传入的字符串不是空的,那么数组中的元素就是以这个字符串相隔开的.
str = [strArray componentsJoinedByString:@""];
NSLog(@"%@",str);
- 4.使用NSCharacterSet(这个贼简单).但是下面要强调一个API.
NSString *str = @" 爱神的箭啥接口的h lahd asdhjkhrjk ahfsdak oauhfdaourfhg 按时UI阿什顿发空间是否会给反馈静安寺 ";
NSCharacterSet *characterSet = [NSCharacterSet whitespaceCharacterSet];
NSLog(@"1:%@",str);
//去除头尾的空格
str = [str stringByTrimmingCharactersInSet:characterSet];
NSLog(@"2:%@",str);
//去除所有的空格
NSArray *array = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
str = [array componentsJoinedByString:@""];
NSLog(@"3:%@",str);
不用我提醒,大家肯定也看到了我上面的那个去除头尾的空格,最开始我用翻NSCharacterSet的API的时候看到了这么一个whitespaceCharacterSet
看字面意义以为是直接去除所有的空格,幸好运行了一下.发现什么都没有去掉.然后看注释的时候发现了只能去掉头尾的空格
NSCharacterset常用API
///属性(点击之后会跳转到底下的相同名字的类方法)
@property (readonly, class, copy) NSCharacterSet *controlCharacterSet; //控制符
@property (readonly, class, copy) NSCharacterSet *whitespaceCharacterSet; //前后两端的空格
@property (readonly, class, copy) NSCharacterSet *whitespaceAndNewlineCharacterSet; //空格和换行
@property (readonly, class, copy) NSCharacterSet *decimalDigitCharacterSet; //小数
@property (readonly, class, copy) NSCharacterSet *letterCharacterSet; //文字
@property (readonly, class, copy) NSCharacterSet *lowercaseLetterCharacterSet; //小写字母
@property (readonly, class, copy) NSCharacterSet *uppercaseLetterCharacterSet; //大写字母
@property (readonly, class, copy) NSCharacterSet *nonBaseCharacterSet; //非基础的所有Unicode字符
@property (readonly, class, copy) NSCharacterSet *alphanumericCharacterSet; //字母数字
@property (readonly, class, copy) NSCharacterSet *decomposableCharacterSet; //可分解
@property (readonly, class, copy) NSCharacterSet *illegalCharacterSet; //非法
@property (readonly, class, copy) NSCharacterSet *punctuationCharacterSet; //标点
@property (readonly, class, copy) NSCharacterSet *capitalizedLetterCharacterSet; //大写
@property (readonly, class, copy) NSCharacterSet *symbolCharacterSet; //
@property (readonly, class, copy) NSCharacterSet *newlineCharacterSet API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)); //换行符
+ (NSCharacterSet *)characterSetWithRange:(NSRange)aRange;
//以下是苹果示例,所有的小写字母
NSRange lcEnglishRange;
NSCharacterSet *lcEnglishLetters;
lcEnglishRange.location = (unsigned int)'a';
lcEnglishRange.length = 26;
lcEnglishLetters = [NSCharacterSet characterSetWithRange:lcEnglishRange];
+ (NSCharacterSet *)characterSetWithCharactersInString:(NSString *)aString;
如果传入一个@"123",会以@"1",@"2",@"3"等分开的字符来判定