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

前端 未结 13 1400
暖寄归人
暖寄归人 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:27

    Just bumped into this, maybe its too late, but here is what worked for me:

    // text is the input string, and this just removes accents from the letters
    
    // lossy encoding turns accented letters into normal letters
    NSMutableData *sanitizedData = [text dataUsingEncoding:NSASCIIStringEncoding
                                      allowLossyConversion:YES];
    
    // increase length by 1 adds a 0 byte (increaseLengthBy 
    // guarantees to fill the new space with 0s), effectively turning 
    // sanitizedData into a c-string
    [sanitizedData increaseLengthBy:1];
    
    // now we just create a string with the c-string in sanitizedData
    NSString *final = [NSString stringWithCString:[sanitizedData bytes]];
    

提交回复
热议问题