Convert special characters like ë,à,é,ä all to e,a,e,a? Objective C

蹲街弑〆低调 提交于 2019-12-03 19:21:19

问题


Is there a simple way in objective c to convert all special characters like ë,à,é,ä to the normal characters like e en a?


回答1:


Yep, and it's pretty simple:

NSString *src = @"Convert special characters like ë,à,é,ä all to e,a,e,a? Objective C";
NSData *temp = [src dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *dst = [[[NSString alloc] initWithData:temp encoding:NSASCIIStringEncoding] autorelease];
NSLog(@"converted: %@", dst);

Running that on my machine produces:

EmptyFoundation[69299:a0f] converted: Convert special characters like e,a,e,a all to e,a,e,a? Objective C

Basically, we're asking the string to transform itself it an NSData (ie, a byte array) that represents the characters in the string in the ASCII character set. Since not all of the characters in the original string are in ASCII, we tell the string that it's OK to do a "lossy" conversion. In other words, it's OK to turn "é" into "e", and so on.

Once we've got our byte array, we simply turn it back into a string, and we're done! :)




回答2:


CFStringTransform

CFStringTransform is the solution when you are dealing with a specific language. It transliterates strings in ways that simplify normalization, indexing, and searching. For example, it can remove accent marks using the option kCFStringTransformStripCombiningMarks:

CFMutableStringRef string = CFStringCreateMutableCopy(NULL, 0, CFSTR("Schläger"));
   CFStringTransform(string, NULL, kCFStringTransformStripCombiningMarks,
     false);
... => string is now “Schlager” CFRelease(string);

CFStringTransform is even more powerful when you are dealing with non-Latin writing systems such as Arabic or Chinese. It can convert many writing systems to Latin script, making normalization much simpler.

For example, you can convert Chinese script to Latin script like this:

CFMutableStringRef string = CFStringCreateMutableCopy(NULL, 0, CFSTR("你好"));
CFStringTransform(string, NULL, kCFStringTransformToLatin, false);
... => string is now “nˇı hˇao”
CFStringTransform(string, NULL, kCFStringTransformStripCombiningMarks,
false);
... => string is now “ni hao” CFRelease(string);

Notice that the option is simply kCFStringTransformToLatin.

The source language is not required. You can hand almost any string to this transform without having to know first what language it is in. CFStringTransform can also transliterate from Latin script to other writing systems such as Arabic, Hangul, Hebrew, and Thai.

References: iOS 7 Programming: Pushing to the limits



来源:https://stackoverflow.com/questions/5217632/convert-special-characters-like-%c3%ab-%c3%a0-%c3%a9-%c3%a4-all-to-e-a-e-a-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!