//判断 中文 大小写英文 数字 特殊字符
NSString *testString = @"中文123ljfLJF";
NSInteger alength = [testString length];
for (int i = 0; i<alength; i++) {
char commitChar = [testString characterAtIndex:i];
NSString *temp = [testString substringWithRange:NSMakeRange(i,1)];
const char *u8Temp = [temp UTF8String];
if (3==strlen(u8Temp)){
NSLog(@"字符串中含有中文");
}else if((commitChar>64)&&(commitChar<91)){
NSLog(@"字符串中含有大写英文字母");
}else if((commitChar>96)&&(commitChar<123)){
NSLog(@"字符串中含有小写英文字母");
}else if((commitChar>47)&&(commitChar<58)){
NSLog(@"字符串中含有数字");
}else{
NSLog(@"字符串中含有非法字符");
}
}
//判断是否为整形:
- (BOOL)isPureInt:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
int val;
return [scan scanInt:&val] && [scan isAtEnd];
}
//判断是否为浮点形:
- (BOOL)isPureFloat:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
float val;
return [scan scanFloat:&val] && [scan isAtEnd];
}
if (![self isPureInt:textField.text] || ![self isPureFloat:textField.text]){
textField.textColor = [UIColor redColor];
textField.text = @"警告:含非法字符,请输入纯数字!";
return;
}else{
NSLog(@"整形或浮点型");
}
// iOS ASCII NSString转换
// NSString to ASCII
NSString *string = @"A";
int asciiCode = [string characterAtIndex:0]; //65
//ASCII to NSString
int asciiCode = 65;
NSString *string =[NSString stringWithFormat:@"%c",asciiCode]; //A
//查找
NSString *string = @"5347858h7";
NSString *pattern = @"[a-zA-Z]";
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
NSArray *results = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
if (results.count = 0) {
NSLog(@"不包含字母");
} else {
NSLog(@"包含了字母");
}
for (NSTextCheckingResult *result in results) {
NSLog(@"范围:%@ 结果:%@", NSStringFromRange(result.range), [string substringWithRange:result.range]);
}
//替换
NSString *string = @"123 &1245; Ross Test 12";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"&[^;]*;" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
NSLog(@"%@", modifiedString);