compare special characters like À with regular A

南笙酒味 提交于 2019-12-02 19:31:15

问题


In some languages there are letters like À, I saw that for table view sections the native iOS put À under the same section as A.
I want to do the same thing, I'm building my sections by comparing the first letter, so I need that À will be equal to A.
I tried using localizedCompare but still I didn't got that those two are equal.

Is there a way to to this kind of compare? or to normalised À so it will be A ?


回答1:


Don't convert or transform as per the other answers. First, try -[NSString localizedStandardCompare:]. If that doesn't do what you want, pass NSDiacriticInsensitiveSearch among the options to one of the NSString comparison methods (such as -compare:options:range:locale:). You may want to use additional options, too, such as NSCaseInsensitiveSearch.




回答2:


You can convert it first into regular alphabets and then compare.

NSString *originalStr = @"À béautiful day";
NSData *d = [originalStr dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *converted = [[NSString alloc] initWithData:d encoding:NSASCIIStringEncoding];



回答3:


Here's a great article about CFStringTransform on NSHipser http://nshipster.com/cfstringtransform/

Next, apply the kCFStringTransformStripCombiningMarks transform to remove any diacritics or accents.



来源:https://stackoverflow.com/questions/22893626/compare-special-characters-like-%c3%80-with-regular-a

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