I have to check out whether a russian character is present in a NSString
or not.
I am using the following code for that:
NSCharacterSet * set =
[[NSCharacterSet characterSetWithCharactersInString:@"БГДЁЖИЙЛПФХЦЧШЩЪЫЭЮЯ"]
invertedSet];
BOOL check = ([nameValue rangeOfCharacterFromSet:set].location == NSNotFound);
return check;
But it is always returning FALSE
.
Can anybody give me an idea what is wrong in my code?
Thanks
I used dasblinkenlight
's answer but I included full Russian alphabet including lowercase characters:
@interface NSString (Russian)
- (BOOL)hasRussianCharacters;
@end
@implementation NSString (Russian)
- (BOOL)hasRussianCharacters{
NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:@"абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"];
return [self rangeOfCharacterFromSet:set].location != NSNotFound;
}
@end
Currently, your condition checks that non-Russian (technically, non-Cyrillic) characters are absent from the string, not that Cyrillic characters are present in the string. Your code will return YES
only for strings that are composed entirely of Cyrillic characters that do not have an equivalent character in the Latin alphabet1.
To fix this problem, remove the inversion, and invert the check, like this:
NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:@"БГДЁЖИЙЛПФХЦЧШЩЪЫЬЭЮЯ"];
return [nameValue rangeOfCharacterFromSet:set].location != NSNotFound;
Ь
in your list, it looks like a lower-case b
, but it is not the same character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With