2012-02-14 75 views
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符号。如何处理?

+0

看起来像重复:http://stackoverflow.com/questions/7656938/iphone-comparing-strings-with-a-german-umlaut – lqez 2012-02-14 16:02:22

+0

不,我不能用'NSDiacriticInsensitiveSearch',因为它不不适用于所有土耳其非拉丁符号。 – 2012-02-15 13:30:05

+0

我找到了解决方案。我可以通过符号代码检查它并将其作为整数进行比较。 – 2012-02-15 13:33:08

回答

3

最后我找到了答案。

unichar是无符号短符号,表示每个符号都有其代码。所以我们可以将它们作为字符而不是数字进行比较。

- (NSComparisonResult) compareTurkishSymbol:(unichar)ch with:(unichar)another 
{ 
    if (ch == 305) {//code of 'ı' 
     ch = 'i'; 
    } 
    return ch - another; 
}