3
我需要实现一种方法,该方法比较两个字符串的相等性,并将某些土耳其字母视为拉丁语(例如,ı= i)。这是计划中的瓶颈,因此需要尽可能高效地实施。目标c中的unichar比较
我不能使用NSString比较:withOption:nsdiactricinsensitivesearch,因为它不能正确使用土耳其字母。
这里是我的算法的实现:
- (NSComparisonResult) compareTurkishSymbol:(unichar)ch with:(unichar)another
{
//needs to be implemented
//code like: if (ch == 'ı') doesn't work correctly
}
- (NSComparisonResult)compareTurkish:(NSString*)word with:(NSString*)another
{
NSUInteger i;
for (i =0; i < word.length; ++i) {
NSComparisonResult result =[self compareTurkishSymbol:[word characterAtIndex:i] with:[another characterAtIndex:i]];
if (result != NSOrderedSame) {
return result;
}
}
return another.length > word.length ? NSOrderedDescending : NSOrderedSame;
}
问题是我无法正确地比较单字符。它不会比较正确的非ASCII符号。如何处理?
看起来像重复:http://stackoverflow.com/questions/7656938/iphone-comparing-strings-with-a-german-umlaut – lqez 2012-02-14 16:02:22
不,我不能用'NSDiacriticInsensitiveSearch',因为它不不适用于所有土耳其非拉丁符号。 – 2012-02-15 13:30:05
我找到了解决方案。我可以通过符号代码检查它并将其作为整数进行比较。 – 2012-02-15 13:33:08