Compare arabic strings with special characters ios

笑着哭i 提交于 2019-12-11 07:36:07

问题


When comparing two arabic strings that have special characters like "إ " "أ" The comparison always fail

NSString* string1 = @"الإجمالي";
NSString* string2 = @"الإجمالي";

BOOL ifEqual ;

if([string1 isEqualToString:string2]){
    ifEqual = YES;
}else{
    ifEqual = NO; //Answer is NO
}

回答1:


The problem you are having is due to isEqualToString: performing a literal comparison, that is the sequence of bytes that make up the two strings must be exactly the same.

Your two strings look the same but are constructed differently, one uses the single Unicode code point for ARABIC LETTER ALEF WITH HAMZA BELOW, the other uses two code points ARABIC LETTER ALEF and ARABIC HAMZA BELOW to produce the same character - these two forms are called precomposed and decomposed respectively.

The standard string compare: family of methods (compare:options:, localizedCompare: et al) default to considering composed characters, the forms which take an option can be set to behave like isEqualToString by specifying NSLiteralSearch.

So just change your code to:

ifEqual = [string1 compare:string2] == NSOrderedSame;

and you will get the answer you expect.




回答2:


The two strings contain the "إ" character in a different representation: The first string as one character ("precomposed representation"):

U+0625 ARABIC LETTER ALEF WITH HAMZA BELOW

and the second string as two characters ("decomposed representation")

U+0627 ARABIC LETTER ALEF
U+0655 ARABIC HAMZA BELOW

If you convert both strings to the precomposed representation then they compare as equal:

NSString* string1 = @"الإجمالي";
NSString* string2 = @"الإجمالي";

string1 = string1.precomposedStringWithCanonicalMapping;
string2 = string2.precomposedStringWithCanonicalMapping;

BOOL ifEqual ;

if ([string1 isEqualToString:string2]) {
    ifEqual = YES; //Answer is YES
} else {
    ifEqual = NO;
}

Swift string comparison handles that automatically:

let string1 = "الإجمالي"
let string2 = "الإجمالي"

print(string1 == string2)
// true


来源:https://stackoverflow.com/questions/43856141/compare-arabic-strings-with-special-characters-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!