NSString - Unicode to ASCII equivalent

后端 未结 5 1032
盖世英雄少女心
盖世英雄少女心 2020-12-05 10:45

I need to convert NSString in unicode to NSString in ASCII changing all local characters: Ą to A, Ś to S, Ó to O, ü to u, And so on...

What is the simplest way to

相关标签:
5条回答
  • 2020-12-05 11:10
    NSString *unicode = @"Chào mừng đến với Việt Nam.";
    NSString *standard = [unicode stringByReplacingOccurrencesOfString:@"đ" withString:@"d"];
    standard = [standard stringByReplacingOccurrencesOfString:@"Đ" withString:@"D"];
    NSData *decode = [standard dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *ansi = [[NSString alloc] initWithData:decode encoding:NSASCIIStringEncoding];
    NSLog(@"ANSI: %@", ansi);
    
    0 讨论(0)
  • 2020-12-05 11:23

    Objective C's NSASCIIEncoding only supports upto 127 , the character set you are looking for are beyond 127 in ASCII table.

    NSASCIIStringEncoding Strict 7-bit ASCII encoding within 8-bit chars; ASCII values 0…127 only. Available in Mac OS X v10.0 and later. Declared in NSString.h.

    0 讨论(0)
  • 2020-12-05 11:29

    Ken answer will replace "æ" with "ae" and "ß" with "s", but won't replace ligatures œ, ij, ff, fi, fl, ffi, ffl, ſt, st, ...

    An improved solution is to first insert additional lines of mapping to handle everything fine:

    string = [string stringByReplacingOccurrencesOfString:@"Œ" withString:@"OE"];
    string = [string stringByReplacingOccurrencesOfString:@"œ" withString:@"oe"];
    string = [string stringByReplacingOccurrencesOfString:@"Đ" withString:@"D"];
    string = [string stringByReplacingOccurrencesOfString:@"đ" withString:@"d"];
    string = [string precomposedStringWithCompatibilityMapping];
    
    NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *newString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    
    0 讨论(0)
  • 2020-12-05 11:31

    -[NSString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES].

    All of the examples you gave are handled as you want. Looks like characters with no obvious analog, such as ☃, go to '?'.

    0 讨论(0)
  • 2020-12-05 11:34
    NSData *data = [decode dataUsingEncoding:[NSString defaultCStringEncoding]];
    decode = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    0 讨论(0)
提交回复
热议问题