比较带有特殊字符的阿拉伯字符串 ios [英] Compare arabic strings with special characters ios
问题描述
比较两个具有特殊字符的阿拉伯字符串时,例如"楸" "棣"比较总是失败
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
}
推荐答案
您遇到的问题是由于 isEqualToString:
执行了 literal 比较,即序列组成两个字符串的字节数必须完全相同.
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.
您的两个字符串看起来相同但构造不同,一个使用单个 Unicode 代码点来表示 ARABIC LETTER ALEF WITH HAMZA BELOW,另一个使用两个代码点 ARABIC LETTER ALEF 和 ARABIC HAMZA BELOW 来生成相同的字符 - 这两个表单分别称为预组合和分解.
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.
标准字符串 compare:
方法系列(compare:options:
, localizedCompare:
et al)默认考虑组合字符,可以通过指定 NSLiteralSearch
将带有选项的表单设置为类似于 isEqualToString
的行为.
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.
这篇关于比较带有特殊字符的阿拉伯字符串 ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!