swift3.0中文转拼音

var chstr = "test"

var code = NSMutableString(string: "东方之珠")  as CFMutableString

//转成拉丁,执行后带音标,code为dōng fāng zhī zhū
if CFStringTransform(code, nil, kCFStringTransformMandarinLatin, false) == true {
    
    //去除音标 ,执行后code为dong fang zhi zhu
    if CFStringTransform(code, nil, kCFStringTransformStripDiacritics, false) == true {
        
        //删除字符串里的空格,执行后chstr为dongfangzhizhu
        chstr = (code as NSString).replacingOccurrences(of: " ", with: "") as  String
        //删除"zhizhu"字符串,执行后chstr为dongfang
        chstr = (chstr as NSString).replacingOccurrences(of: "zhizhu", with: "") as  String

        print(chstr)  //输出 dongfang
    }
}

你可能感兴趣的:(swift3.0中文转拼音)