NSString - Unicode to ASCII equivalent

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];

-[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 '?'.


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);