Conversion of Chinese character string in OC into the sorting of pinyin character string

Source: Internet
Author: User
Tags ming

When the string in OC is a Chinese character, we often want to do string comparisons, but the Chinese characters can not be compared, so it is necessary to convert the Chinese characters into pinyin, the specific process is as follows:

A mutable string must be a mutable string.
nsmutablestring *mutablestring = [nsmutablestring stringwithstring:@ "Xiao Ming"];
Turn into a tonal phonetic alphabet
Cfstringtransform ((cfmutablestringref) mutablestring, NULL, Kcfstringtransformtolatin, false);
NSLog (@ "%@", mutablestring);

The result: Xiǎo Míng

Turn into a phonetic alphabet without tones
Cfstringtransform ((cfmutablestringref) mutablestring, NULL, Kcfstringtransformstripdiacritics, false);

The output is: Xiao Ming
NSLog (@ "%@", mutablestring);

Note: This method must first convert the immutable string to a mutable string in order to convert the kanji into pinyin.

There is a flaw in this approach: when encountering multi-tone words, it is not so only, can only be converted into a pinyin

In addition: You also see the conversion of Pinyin will appear between the space, so you want to replace the space:

NSString *str = @ "Xiao Ming Ming";
NSString *newstr = [str stringbyreplacingoccurrencesofstring:@ "" withstring:@ ""];

But a new string is returned after the substitution, and the original string has not changed!, you will use the newly returned string to perform the operation!!!

There are drawbacks, but you can also use the!!!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Conversion of Chinese character string in OC into the sorting of pinyin character string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.