IOS簡單的字串替換方法stringByTrimmingCharactersInSet


此方法只能過濾掉首尾,但是條件是集合

今天聽人介紹了一個比較簡單的過濾方法...不多說了,直接上代碼

 1 NSString *str = @"一個 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 漢0932字#@!中..文//>?輸~~~@#$#@%#^#^%&^*&(*)入";
 2     NSMutableCharacterSet *set = [[NSMutableCharacterSet alloc] init];
 3     [set formUnionWithCharacterSet:[NSCharacterSet lowercaseLetterCharacterSet]];//小寫字母
 4     NSLog(@"1 %@",[str stringByTrimmingCharactersInSet:set]);
 5     [set formUnionWithCharacterSet:[NSCharacterSet uppercaseLetterCharacterSet]];//大寫字母
 6     NSLog(@"2 %@",[str stringByTrimmingCharactersInSet:set]);
 7     [set formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];//符號
 8     NSLog(@"3 %@",[str stringByTrimmingCharactersInSet:set]);
 9     [set formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];//標點
10     NSLog(@"4 %@",[str stringByTrimmingCharactersInSet:set]);
11     [set formUnionWithCharacterSet:[NSCharacterSet controlCharacterSet]];//控制符
12     NSLog(@"5 %@",[str stringByTrimmingCharactersInSet:set]);
13     [set formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];//小數
14     NSLog(@"6 %@",[str stringByTrimmingCharactersInSet:set]);
15     [set formUnionWithCharacterSet:[NSCharacterSet letterCharacterSet]];//文字
16     NSLog(@"7 %@",[str stringByTrimmingCharactersInSet:set]);
17     [set formUnionWithCharacterSet:[NSCharacterSet nonBaseCharacterSet]];//非基礎
18     NSLog(@"8 %@",[str stringByTrimmingCharactersInSet:set]);
19     [set formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];//字母數字
20     NSLog(@"9 %@",[str stringByTrimmingCharactersInSet:set]);
21     [set formUnionWithCharacterSet:[NSCharacterSet decomposableCharacterSet]];//可分解
22     NSLog(@"10 %@",[str stringByTrimmingCharactersInSet:set]);
23     [set formUnionWithCharacterSet:[NSCharacterSet illegalCharacterSet]];//非法
24     NSLog(@"11 %@",[str stringByTrimmingCharactersInSet:set]);
25     [set formUnionWithCharacterSet:[NSCharacterSet capitalizedLetterCharacterSet]];//大寫
26     NSLog(@"12 %@",[str stringByTrimmingCharactersInSet:set]);
27     [set formUnionWithCharacterSet:[NSCharacterSet newlineCharacterSet]];//換行符
28     NSLog(@"13 %@",[str stringByTrimmingCharactersInSet:set]);
29     [set formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//空格換行
30     NSLog(@"14 %@",[str stringByTrimmingCharactersInSet:set]);

 

 

 1 2012-07-26 19:11:01.863 yingkong1987[12876:fb03] 1 一個 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 漢0932字#@!中..文//>?輸~~~@#$#@%#^#^%&^*&(*)入
 2 2012-07-26 19:11:01.864 yingkong1987[12876:fb03] 2 一個 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 漢0932字#@!中..文//>?輸~~~@#$#@%#^#^%&^*&(*)入
 3 2012-07-26 19:11:01.864 yingkong1987[12876:fb03] 3 一個 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 漢0932字#@!中..文//>?輸~~~@#$#@%#^#^%&^*&(*)入
 4 2012-07-26 19:11:01.864 yingkong1987[12876:fb03] 4 一個 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 漢0932字#@!中..文//>?輸~~~@#$#@%#^#^%&^*&(*)入
 5 2012-07-26 19:11:01.865 yingkong1987[12876:fb03] 5 一個 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 漢0932字#@!中..文//>?輸~~~@#$#@%#^#^%&^*&(*)入
 6 2012-07-26 19:11:01.865 yingkong1987[12876:fb03] 6 一個 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 漢0932字#@!中..文//>?輸~~~@#$#@%#^#^%&^*&(*)入
 7 2012-07-26 19:11:01.865 yingkong1987[12876:fb03] 7  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
 8 2012-07-26 19:11:01.865 yingkong1987[12876:fb03] 8  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
 9 2012-07-26 19:11:01.865 yingkong1987[12876:fb03] 9  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
10 2012-07-26 19:11:01.866 yingkong1987[12876:fb03] 10  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
11 2012-07-26 19:11:02.170 yingkong1987[12876:fb03] 11  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
12 2012-07-26 19:11:02.170 yingkong1987[12876:fb03] 12  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
13 2012-07-26 19:11:02.170 yingkong1987[12876:fb03] 13  (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 
14 2012-07-26 19:11:02.170 yingkong1987[12876:fb03] 14 

發現這個方法有一個很大的缺點..比如要去掉(ABC)的英文部分..必須要先去掉外面的括號....不然無法過濾...

空格亦是如此

如果有更好的過濾方法,歡迎留言討論


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM