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