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