最新消息:服务器现已从linode换到fzhost.net,且主题已从retina 0.2换为大前端D8

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

Swift crifan 23浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
51 queries in 0.341 seconds, using 12.63MB memory