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

前端 未结 2 1491
攒了一身酷
攒了一身酷 2020-12-31 05:40

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

2条回答
  •  别那么骄傲
    2020-12-31 06:14

    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

提交回复
热议问题