[已解决]swift 提取中文汉字的拼音的第一个字母

需要用swift实现,从一个汉字的拼音的第一个字母
比如:张三
提取出来的是:(姓的拼音zhang的第一个字母:z)
搜:
swift chinese char first letter
参考:
搜:
swift chinese characters pinyin
ios chinese characters pinyin
ios get pinyin of chinese characters
ios Chinese characters to english letters
ios Chinese characters letters
ios contact first letter
object-c chinese contact name to Alphabetical
object-c chinese contact to Alphabetical
object-c chinese  Alphabetical
参考:
问题转化为:
如果将汉字字符串,转换为拼音
得到拼音后,即可获取首字母了。
参考:
参考:
kCFStringTransformStripCombiningMarks
The identifier of a transform to strip combining marks (accents or diacritics).
kCFStringTransformStripDiacritics
The identifier of a transform to remove diacritic markings.
用代码:
//: Playground - noun: a place where people can play
//Crifan: test translate zh-cn Chinese string into PinYin

import UIKit

var zhcnNameList = ["张三", "李四", "王五", "徐六", "赵七", "王八",]
print(zhcnNameList)

func translateChineseStringToPyinyin(chineseStr:String) -> String {
    print("chineseStr=\(chineseStr)") //chineseStr=王八

    var translatedPinyinStr:String = ""

    let zhcnStrToTranslate:CFMutableStringRef = NSMutableString(string: chineseStr)
    print("zhcnStrToTranslate=\(zhcnStrToTranslate)") //zhcnStrToTranslate=王八

    var translatedOk:Bool = CFStringTransform(zhcnStrToTranslate, nil, kCFStringTransformMandarinLatin, false)
    print("translatedOk=\(translatedOk)") //translatedOk=true
    print("zhcnStrToTranslate=\(zhcnStrToTranslate)") //zhcnStrToTranslate=wáng bā

    if translatedOk {
        //continue to translated to pinyin without accents
        let translatedPinyinWithAccents = zhcnStrToTranslate
        translatedOk = CFStringTransform(translatedPinyinWithAccents, nil, kCFStringTransformStripCombiningMarks, false)

        if translatedOk {
            translatedPinyinStr = translatedPinyinWithAccents as String
        }
    }

    print("translatedPinyinStr=\(translatedPinyinStr)") //translatedPinyinStr=wang ba
    return translatedPinyinStr
}

for (idx, eachZhcnName) in zhcnNameList.enumerate() {
    print("[\(idx)] \(eachZhcnName)") //[5] 王八
    let translatedPinyinStr = translateChineseStringToPyinyin(zhcnNameList[idx])
    print("translatedPinyinStr=\(translatedPinyinStr)")
}
效果:
chinese char to pinyin crifan playground
抽空继续去优化:
也去建一个字典缓存,以便提高加载速度。
-》抽空倒是需要去搞清楚Xcode中如何调试查看代码执行时间呢
后续继续优化代码为:
//translate chinese string into pinyin with accents
func translateChineseStrToPinyinWithAccents(chineseStr:String) -> String {
    print("chineseStr=\(chineseStr)") //chineseStr=王八

    var translatedPinyinWithAccents:String = ""

    let zhcnStrToTranslate:CFMutableStringRef = NSMutableString(string: chineseStr)
    print("zhcnStrToTranslate=\(zhcnStrToTranslate)") //zhcnStrToTranslate=王八

    let translatedOk:Bool = CFStringTransform(zhcnStrToTranslate, nil, kCFStringTransformMandarinLatin, false)
    print("translatedOk=\(translatedOk)") //translatedOk=true

    if translatedOk {
        translatedPinyinWithAccents = zhcnStrToTranslate as String
        print("translatedPinyinWithAccents=\(translatedPinyinWithAccents)")
    }

    return translatedPinyinWithAccents
}

//remove accents from (Chinese PinYin) string
func removeAccentsFromStr(strWithAccents:String) -> String {
    print("strWithAccents=\(strWithAccents)") //áng shān sù jì

    var removedAccentsStr:String = ""

    let strWithAccentsRef:CFMutableStringRef = NSMutableString(string: strWithAccents)

    //method 1: kCFStringTransformStripCombiningMarks
    let translatedOk = CFStringTransform(strWithAccentsRef, nil, kCFStringTransformStripCombiningMarks, false)
    //    //method 2: kCFStringTransformStripDiacritics
    //    let translatedOk = CFStringTransform(strWithAccentsRef, nil, kCFStringTransformStripDiacritics, false)
    print("translatedOk=\(translatedOk)") //true

    if translatedOk {
        removedAccentsStr = strWithAccentsRef as String
        print("removedAccentsStr=\(removedAccentsStr)") //ang shan su ji
    }

    return removedAccentsStr
}

//translate Chinese characters string to characterStr:pinyin(without accents) dictionary list
//Note: here use dict list intead of dict to makesure returned key:value sequence can guaranteed
func translateChineseStrToCharPinyinDict(chineseStr:String) -> [[String:String]] {
    //print("chineseStr=\(chineseStr)") //昂山素季

    let noneSeperatorStr:String = ""
    let chinseSingleCharStrArr:[String] = splitSingleStrToStrArr(chineseStr, seperatorStr: noneSeperatorStr)
    //print("chinseSingleCharStrArr=\(chinseSingleCharStrArr)") //["昂", "山", "素", "季"]

    var translatedCharPinyinDictList:[[String:String]] = [[String:String]]()

    let translatedPinyinStrWithAccents:String = translateChineseStrToPinyinWithAccents(chineseStr)
    //print("translatedPinyinStrWithAccents=\(translatedPinyinStrWithAccents)") //áng shān sù jì

    let pinyinStrWithoutAccents:String = removeAccentsFromStr(translatedPinyinStrWithAccents)
    //print("pinyinStrWithoutAccents=\(pinyinStrWithoutAccents)") //ang shan su ji

    let spaceSeperatorStr:String = " "
    var pinyinStrArr:[String] = splitSingleStrToStrArr(pinyinStrWithoutAccents, seperatorStr: spaceSeperatorStr)
    //print("pinyinStrArr=\(pinyinStrArr)") //["ang", "shan", "su", "ji"]

    for (idx, curSingleChineseCharStr) in chinseSingleCharStrArr.enumerate() {
        let curPinyinStr:String = pinyinStrArr[idx]
        translatedCharPinyinDictList.append([curSingleChineseCharStr: curPinyinStr])
    }

    //print("translatedCharPinyinDictList=\(translatedCharPinyinDictList)") //[["昂": "ang"], ["山": "shan"], ["素": "su"], ["季": "ji"]]
    return translatedCharPinyinDictList
}
使用举例:
    /*
    * translateChineseStrToPinyinWithAccents demo
    */
    let chineseStr:String = "昂山素季"
    let translatedPinyinStrWithAccents:String = translateChineseStrToPinyinWithAccents(chineseStr)
    print("translatedPinyinStrWithAccents=\(translatedPinyinStrWithAccents)") //"áng shān sù jì"

    /*
    * removeAccentsFromStr demo
    */
    let pinyinWithAccents:String = "áng shān sù jì"
    let strippedAccentsPinyin:String = removeAccentsFromStr(pinyinWithAccents)
    print("strippedAccentsPinyin=\(strippedAccentsPinyin)") //"ang shan su ji"

    /*
    * translateChineseStrToCharPinyinDict demo
    */
    let chineseStrToTranslate:String = "昂山素季"
    let translatedCharPinyinDict = translateChineseStrToCharPinyinDict(chineseStrToTranslate)
    print("translatedCharPinyinDict=\(translatedCharPinyinDict)") //[["昂": "ang"], ["山": "shan"], ["素": "su"], ["季": "ji"]]
更多相关函数可参考:


发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量