iOS ~ 判断字符是什么(Unicode)中文、英文、俄文等等
2021-11-03 本文已影响0人
阳光下的叶子呵
Unicode 字符百科
纯中文:
//是否是纯汉字
- (BOOL)isChinese {
NSString *match = @"(^[\u4e00-\u9fa5]+$)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
return [predicate evaluateWithObject:self];
}
包含中文:
//是否包含汉字
- (BOOL)includeChinese {
for(int i=0; i< [self length];i++)
{
int a =[self characterAtIndex:i];
if( a >0x4e00&& a <0x9fff){
return YES;
}
}
return NO;
}
英文:
// 判断是否以字母开头
- (BOOL)isEnglishFirst:(NSString *)str {
NSString *regular = @"^[A-Za-z].+$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regular];
if ([predicate evaluateWithObject:str] == YES){
return YES;
}else{
return NO;
}
}
俄文:
// 判断一个字符是否是字母
+(BOOL)JudgeString:(NSString *)string {
NSString *regex =@"^[\u0410-\u044F]+$";
NSPredicate*predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return[predicate evaluateWithObject:string];
}