iPhone - Comparing strings with a German umlaut

前端 未结 1 746
北荒
北荒 2021-01-06 09:05

I\'ve few German strings (with umlauts like åä etc) in NSArray. For example consider a word like \"gënder\" is there in array. User enters \"gen\" in a text field. I can to

相关标签:
1条回答
  • 2021-01-06 10:02

    Use the NSDiacriticInsensitiveSearch option of the various NSString compare methods. As described in the documentation:

    Search ignores diacritic marks. For example, ‘ö’ is equal to ‘o’.

    For example:

    NSString *text = @"gënder";
    NSString *searchString = @"ender";
    
    NSRange rng = [text rangeOfString:searchString 
                              options:NSDiacriticInsensitiveSearch];
    
    if (rng.location != NSNotFound)
    {
        NSLog(@"Match at %@", NSStringFromRange(rng));
    }
    else
    {
        NSLog(@"No match");
    }
    
    0 讨论(0)
提交回复
热议问题