Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if an NSString is latin based?

I'm trying to determine if a string is latin based or Japanese.

I've tried something like the following but it returns YES for Japanese strings as well:

NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
BOOL isAlpha = [[myStr stringByTrimmingCharactersInSet:alphaSet] isEqualToString:@""];

A string might be a word like "café" or something like "カフェ" or "喫茶店".

like image 586
Louis Waweru Avatar asked Mar 24 '11 06:03

Louis Waweru


1 Answers

Use the canBeConvertedToEncoding: method. For example:

BOOL isLatin = [myString canBeConvertedToEncoding:NSISOLatin1StringEncoding];

Available encodings are here.

like image 94
Darren Avatar answered Oct 19 '22 05:10

Darren