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 have to do a string comparison, but the Chinese characters can not be compared, so we will convert the Chinese characters into pinyin, detailed steps such as the following:

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 an immutable string into a mutable string to convert the kanji into pinyin.

Also, there is a flaw in this approach: when it comes to multi-tone words, it's not so much that it just translates into a phonetic alphabet.

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 the replacement is returned by a new string, the original string has not changed!, you will use the newly returned string to perform the operation!!!

Although there are flaws, but can also be used to use the!!!


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.