圖片源自網(wǎng)絡(luò)
NSCharacterSet
NSCharacterSet & NSMutableCharacterSet: 用面向?qū)ο蟮姆绞絹肀硎疽唤MUnicode字符蕴忆。
經(jīng)常與NSString及NSScanner組合起來使用午笛,在不同的字符上做過濾惩系、刪除或者分割操作舵揭。
NSCharacterSet使用
-
系統(tǒng)提供的常用的創(chuàng)建NSCharacterSet的類方法
[NSCharacterSet controlCharacterSet]; //控制符的字符集 [NSCharacterSet whitespaceCharacterSet]; //空格的字符集 [NSCharacterSet whitespaceAndNewlineCharacterSet]; //空格和換行符的字符集 [NSCharacterSet decimalDigitCharacterSet]; //十進制數(shù)字的字符集 [NSCharacterSet letterCharacterSet]; //字母的字符集 [NSCharacterSet lowercaseLetterCharacterSet]; //小寫字母的字符集 [NSCharacterSet uppercaseLetterCharacterSet]; //大寫字母的字符集 [NSCharacterSet nonBaseCharacterSet]; //非基礎(chǔ)的字符集 [NSCharacterSet alphanumericCharacterSet]; //字母和數(shù)字的字符集 [NSCharacterSet decomposableCharacterSet]; //可分解 [NSCharacterSet illegalCharacterSet]; //非法的字符集 [NSCharacterSet punctuationCharacterSet]; //標點的字符集 [NSCharacterSet capitalizedLetterCharacterSet]; //首字母大寫的字符集 [NSCharacterSet symbolCharacterSet]; //符號的字符集 [NSCharacterSet newlineCharacterSet]; //換行符的字符集
-
通過自定義創(chuàng)建NSCharacterSet的方法
//返回一個指定范圍的字符集,取自小寫字母字符集 + (NSCharacterSet *)characterSetWithRange:(NSRange)aRange; //返回一個包含當前字符串的字符集 + (NSCharacterSet *)characterSetWithCharactersInString:(NSString *)aString; //返回包含由給定位圖表示形式確定的字符的字符集,此方法對于使用來自文件或其他外部數(shù)據(jù)源的數(shù)據(jù)創(chuàng)建字符集 + (NSCharacterSet *)characterSetWithBitmapRepresentation:(NSData *)data; //返回從位圖表示中讀取的字符集,存儲在文件中給定的路徑。 + (nullable NSCharacterSet *)characterSetWithContentsOfFile:(NSString *)fName;
例如:
[NSCharacterSet characterSetWithCharactersInString:@"Hello"]; //String中的文字組成的set [NSCharacterSet characterSetWithRange:NSMakeRange(48, 10)]];//ascll碼校赤,此處為(0-9) [[NSCharacterSet decimalDigitCharacterSet] invertedSet];//數(shù)字之外的字符組成的set
-
判斷一個字符是否在set中
//指定字符集是包含于在于當前字符集 - (BOOL)characterIsMember:(unichar)aCharacter;
例如:
[[NSCharacterSet decimalDigitCharacterSet] characterIsMember:48]; //48是0的ascll碼吆玖;
舉例使用
- 去掉首尾空格
NSString *testString = @" This is the string contains whitespace in beginning and ending ";
NSString *whitesspaceStr = [testString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@",whitesspaceStr);
- 去除首尾指定字符串
NSString *str=@"哈哈呵呵嘿嘿吼吼";
NSCharacterSet *cs= [NSCharacterSet characterSetWithCharactersInString:@"哈吼"];
NSString *strResult = [str stringByTrimmingCharactersInSet:cs];
NSLog(@"%@",strResult);
- 用指定字符串替代當前字符中的指定字符集中的字符串
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:@"_"]);
- 去除所有空格
NSString *string = @" a b cd ef gh ij klm nopq rstu v w x y z ";
NSLog(@"%@",[[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""]);
- 與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);
- 判斷字符串是否只包含數(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;
}
- 在UITextFieldDelegate方法中, 限制只能輸入數(shù)字和小數(shù)點, 且第一位不可以輸入小數(shù)點, 小數(shù)點只能輸入一個
- (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;
}
注意事項
盡可能的少創(chuàng)建可變字符設(shè)置(character sets).
緩存character sets筒溃,而不是不斷重新創(chuàng)建character sets.
避免歸檔character sets作為對象,但可以存儲到character sets文件當中 (歸檔將被復制到不同的歸檔文件,將導致磁盤空間的浪費)
當創(chuàng)建一個自定義sets時,不需要在創(chuàng)建之后進行改變,使用一個不可變character set作為最終的使用,或創(chuàng)建一個character set文件并存儲到應用中.
參考
學會NSCharacterSet,再也不怕各種字符串處理U闯恕怜奖!