【问题标题】:unichar comparision in objective c目标 c 中的 unichar 比较
【发布时间】:2012-02-14 13:27:25
【问题描述】:

我需要实现一个方法,它比较两个字符串是否相等,将一些土耳其字母视为拉丁字母(例如 ı = i)。这是程序中的瓶颈,因此需要尽可能高效地实施。

我不能使用 NSString compare: 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;
}

问题是我无法正确比较 unichars。它不能正确比较非 ascii 符号。怎么处理?

【问题讨论】:

  • 没有。我不能使用NSDiacriticInsensitiveSearch,因为它不适用于所有土耳其语非拉丁符号。
  • 我找到了解决方案。我可以通过符号代码检查它并作为整数进行比较。

标签: objective-c unichar


【解决方案1】:

终于找到了答案。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多