美文网首页
swift3.0中文转拼音

swift3.0中文转拼音

作者: yytester | 来源:发表于2016-12-12 16:54 被阅读57次
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中文转拼音

      本文链接:https://www.haomeiwen.com/subject/nwgmmttx.html