[已解决]swift把字符串数组或字符数组合并为单个字符串

需要用swift实现,将字符串数组合并为单个的字符串
最后直接+即可:
//merge character array into string
func mergeCharArrToSingleStr(charArr:[Character]) -> String {
    print("charArr=\(charArr)") //charArr=["一", "个", "字", "符", "串"]

    let mergedSingleStr:String = String(charArr) //"一个字符串"
    print("mergedSingleStr=\(mergedSingleStr)")

    return mergedSingleStr
}

//merge string array into single string
func mergeStrArrToSingleStr(strArr:[String]) -> String {
    print("strArr=\(strArr)")

    var singleStr:String = ""

    for eachStr in strArr {
        singleStr += eachStr
    }
    print("singleStr\(singleStr)")

    return singleStr
}

调用示例代码:

    /*
    * mergeCharArrToSingleStr demo
    */
    let charArrToMerge:[Character] = ["待", "合", "并", "字", "符", "数", "组"]
    let mergedStr:String = mergeCharArrToSingleStr(charArrToMerge)
    print("mergedStr=\(mergedStr)") //"待合并字符数组"

    /*
    * mergeStrArrToSingleStr demo
    */
    let stringArrToMerge:[String] = ["待", "合", "并", "字", "符", "串","数", "组"]
    let mergedSingleStr:String = mergeStrArrToSingleStr(stringArrToMerge)
    print("mergedSingleStr=\(mergedSingleStr)") //"待合并字符串数组"

 



发表评论

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

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