首页 > 代码库 > iOS 判断内容是否是中文,两种实现

iOS 判断内容是否是中文,两种实现

用category实现

新建类别文件,代码

.h文件

#import <Foundation/Foundation.h>@interface NSString (Valid)- (BOOL)isChinese;@end

 

.m文件

#import "NSString+Valid.h"@implementation NSString (Valid)- (BOOL)isChinese{    NSString *match = @"(^[\u4e00-\u9fa5]+$)";    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];    return [predicate evaluateWithObject:self];}@end

用法:

if ([self.usernameTextField.text isChinese]) {      //  do      }

  

还有一种是直接写公用方法:

- (BOOL)isChinese:(NSString *)str{    NSString *match = @"(^[\u4e00-\u9fa5]+$)";    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];    return [predicate evaluateWithObject:str];}

  

  

 

全文结束~

 

iOS 判断内容是否是中文,两种实现