比较带有特殊字符ios的阿拉伯字符串 [英] Compare arabic strings with special characters ios

查看:176
本文介绍了比较带有特殊字符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代码点表示带有HAMZA BELOW的ARABIC LETTER ALEF,另一个字符串使用两个代码点生成两个字符相同的字符-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: )默认考虑组合字符,采用选项的形式可以设置为类似于通过指定NSLiteralSearch.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆