Detecting Asian characters in a string

With the simple use of a regular expression to find out if a string contains Chinese, Japanese or Korean (aka CJK) characters.


#import <Foundation/Foundation.h>
@interface NSString (CJK)
/**
Detect if the string contains Chinese, Japanese or Korean characters
@return YES if the string contains CJK characters, NO otherwise
*/
– (BOOL)containsAsianCharacters;
@end

view raw

NSString+CJK.h

hosted with ❤ by GitHub


#import "NSString+CJK.h"
@implementation NSString (CJK)
– (BOOL)containsAsianCharacters
{
if (![self length])
return NO;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\u2E80-\u9FFF]" options:NSRegularExpressionCaseInsensitive error:nil];
return ([regex matchesInString:self options:0 range:NSMakeRange(0, [self length])]);
}
@end

view raw

NSString+CJK.m

hosted with ❤ by GitHub


One thought on “Detecting Asian characters in a string

Leave a Reply

Your email address will not be published. Required fields are marked *