需要用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把字符串数组或字符数组合并为单个字符串