问题:
ios 怎么把汉字转换成拼音?
解决:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
import Foundation extension String { func transformToPinYin() -> String { let mutableString = NSMutableString(string: self) //把汉字转为拼音 CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false) //去掉拼音的音标 //CFStringTransform(mutableString, nil, kCFStringTransformStripDiacritics, false) let string = String(mutableString) //去掉空格 //return string.stringByReplacingOccurrencesOfString(" ", withString: "") return string.replacingOccurrences(of: " ", with: "") } } |
print(“中国”.tranformToPinYin()) 输出 :
zhōngguó
参考:
http://blog.csdn.net/potato512/article/details/51108815