NSString - Convert to pure alphabet only (i.e. remove accents+punctuation)

前端 未结 13 1480
暖寄归人
暖寄归人 2020-12-02 15:49

I\'m trying to compare names without any punctuation, spaces, accents etc. At the moment I am doing the following:

-(NSString*) prepareString:(NSString*)a {
         


        
13条回答
  •  误落风尘
    2020-12-02 16:32

    On a similar question, Ole Begemann suggests using stringByFoldingWithOptions: and I believe this is the best solution here:

    NSString *accentedString = @"ÁlgeBra";
    NSString *unaccentedString = [accentedString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
    

    Depending on the nature of the strings you want to convert, you might want to set a fixed locale (e.g. English) instead of using the user's current locale. That way, you can be sure to get the same results on every machine.

提交回复
热议问题