最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Swift crifan 3447浏览 0评论
需要用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)") //"待合并字符串数组"

 

转载请注明:在路上 » [已解决]swift把字符串数组或字符数组合并为单个字符串

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.170 seconds, using 22.10MB memory