2012-02-14 74 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; 
}