簡介
NSCharacterSet ,以及它的可變類型 NSMutableCharacterSet,用面向?qū)ο蟮姆绞絹肀硎疽唤MUnicode字符。它經(jīng)常與NSString及NSScanner組合起來使用,在不同的字符上做過濾、刪除或者分割操作。
方法和屬性介紹:
NSCharacterSet 中提供的下面屬性都是只讀的, 且在NSMutableCharacterSet中有一致的類方法
1.controlCharacterSet //控制符的字符集
2.whitespaceCharacterSet //空格的字符集
3.whitespaceAndNewlineCharacterSet //空格和換行符的字符集
4.decimalDigitCharacterSet //十進(jìn)制數(shù)字的字符集
5.letterCharacterSet //字母的字符集
6.lowercaseLetterCharacterSet //小寫字母的字符集
7.uppercaseLetterCharacterSet //大寫字母的字符集
8.nonBaseCharacterSet //非基礎(chǔ)的字符集
9.alphanumericCharacterSet //字母和數(shù)字的字符集
10.decomposableCharacterSet //可分解
11.illegalCharacterSet //非法的字符集
12.punctuationCharacterSet //標(biāo)點(diǎn)的字符集
13.capitalizedLetterCharacterSet //首字母大寫的字符集
14.symbolCharacterSet //符號的字符集
15.newlineCharacterSet //換行符的字符集
NSCharacterSet 和 NSMutableCharacterSet一致的類方法
//返回一個(gè)指定范圍的字符集,取自小寫字母字符集
+ (NSCharacterSet *)characterSetWithRange:(NSRange)aRange;
//返回一個(gè)包含當(dāng)前字符串的字符集
+ (NSCharacterSet *)characterSetWithCharactersInString:(NSString *)aString;
//返回包含由給定位圖表示形式確定的字符的字符集,此方法對于使用來自文件或其他外部數(shù)據(jù)源的數(shù)據(jù)創(chuàng)建字符集
+ (NSCharacterSet *)characterSetWithBitmapRepresentation:(NSData *)data;
//返回從位圖表示中讀取的字符集,存儲在文件中給定的路徑。
+ (nullable NSCharacterSet *)characterSetWithContentsOfFile:(NSString *)fName;
NSCharacterSet中的其它方法或?qū)傩裕?/b>
//指定字符集是包含于在于當(dāng)前字符集
- (BOOL)characterIsMember:(unichar)aCharacter;
//以二進(jìn)制格式編碼接收器的NSData對象,此格式適用于保存到文件或以其他方式傳輸或歸檔
@property (readonly, copy) NSData *bitmapRepresentation;
//反轉(zhuǎn)字符集,僅包含當(dāng)前字符集中不存在的字符
@property (readonly, copy) NSCharacterSet *invertedSet;
NSMutableCharacterSet中其它方法:
- (void)addCharactersInRange:(NSRange)aRange;
- (void)removeCharactersInRange:(NSRange)aRange;
- (void)addCharactersInString:(NSString *)aString;
- (void)removeCharactersInString:(NSString *)aString;
- (void)formUnionWithCharacterSet:(NSCharacterSet *)otherSet;
- (void)formIntersectionWithCharacterSet:(NSCharacterSet *)otherSet;
- (void)invert;
//*****************************************
延伸常與NSString 的一些方法結(jié)合使用, 來達(dá)到某些效果
//返回一個(gè)指定字符集分隔開的子字符串?dāng)?shù)組
- (NSArray*)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator NS_AVAILABLE(10_5, 2_0);
//返回一個(gè)去除兩端指定字符集的字符串
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;
//返回指定字符集在當(dāng)前字符串中的第一個(gè)符合條件的范圍
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet;
NSArray的一些方法:
//在數(shù)組中子串之間插入指定字符
- (NSString *)componentsJoinedByString:(NSString *)separator;
應(yīng)用
NSString *testString = @"This is the test string for %a*b*c&";
NSArray *divArr = [testString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"abc"]];
NSLog(@"%@",divArr);
打印結(jié)果:
(
"This is the test string for %",
"*",
"*",
"&"
)
舉例使用
1.去掉首尾空格
NSString *testString = @"? ? ? This is the string contains whitespace in beginning and ending? ? ";
NSString *whitesspaceStr = [testString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@",whitesspaceStr);
打印結(jié)果: This is the string contains whitespace in beginning and ending
2.去除首尾指定字符串
NSString *str=@"哈哈呵呵嘿嘿吼吼";
NSCharacterSet *cs= [NSCharacterSet characterSetWithCharactersInString:@"哈吼"];
NSString *strResult = [str stringByTrimmingCharactersInSet:cs];
NSLog(@"%@",strResult);
打印結(jié)果: 呵呵嘿嘿
3.用指定字符串替代當(dāng)前字符中的指定字符集中的字符串
NSMutableCharacterSet *letter = [NSMutableCharacterSet lowercaseLetterCharacterSet];
NSCharacterSet *decimalDigit = [NSCharacterSet decimalDigitCharacterSet];
[letter formUnionWithCharacterSet:decimalDigit];
NSString *string = @"g8!hgr3@09#23uiq%^78sjn453t78&13gesg*wt53(545y45)q3at";
NSLog(@"%@",[[string componentsSeparatedByCharactersInSet:letter] componentsJoinedByString:@"_"]);
[letter invert];? //字母數(shù)字反轉(zhuǎn)
NSLog(@"%@",[[string componentsSeparatedByCharactersInSet:letter] componentsJoinedByString:@"_"]);
打印結(jié)果:
__!____@__#_____%^___________&______*____(______)____
g8_hgr3_09_23uiq__78sjn453t78_13gesg_wt53_545y45_q3at
4.去除所有空格
NSString *string = @"? a b? cd? ef gh ij? ? klm? nopq rstu v? w x? y z? ";
NSLog(@"%@",[[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""]);
打印結(jié)果: abcdefghijklmnopqrstuvwxyz
5.與NSPredicate結(jié)合使用壓縮空格
NSString *string = @"? Additional? ? setup? after? ? loading the? ? view.";
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *components = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
components = [components filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self <> ''"]];
string = [components componentsJoinedByString:@" "];
NSLog(@"%@", string);
打印結(jié)果: Additional setup after loading the view.
6.判斷字符串是否只包含數(shù)字
- (BOOL)validateNumber:(NSString*)number {
BOOL res = YES;
NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
int i = 0;
while (i < number.length) {
NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
NSRange range = [string rangeOfCharacterFromSet:tmpSet];
if (range.length == 0) {
res = NO;
break;
}
i++;
}
return res;
}
7.在UITextFieldDelegate方法中, 限制只能輸入數(shù)字和小數(shù)點(diǎn), 且第一位不可以輸入小數(shù)點(diǎn), 小數(shù)點(diǎn)只能輸入一個(gè)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *cs;
NSUInteger nDotLoc = [textField.text rangeOfString:@"."].location;
if (NSNotFound == nDotLoc && 0 != range.location) {
cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
}else{
cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
}
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOL basicTest = [string isEqualToString:filtered];
if (!basicTest) {
return NO;
}
return YES;
}