如何在Swift中将字符串数组转换为字符串

时间:2018-09-12 11:05:52

标签: arrays swift

我需要将String数组转换为单个String

这是我的数组:

  

[“ 1”,“ 2”,“ 3”,“ 4”,“ 5”]

我需要得到这个:

  

“ [” 1“,” 2“,” 3“,” 4“,” 5“]”

7 个答案:

答案 0 :(得分:3)

结果是一个JSON字符串,请使用JSONEncoder

let array =  ["1","2","3","4","5"]
do {
    let data = try JSONEncoder().encode(array)
    let string = String(data: data, encoding: .utf8)!
    print(string.debugDescription) // "[\"1\",\"2\",\"3\",\"4\",\"5\"]"
} catch { print(error) }

答案 1 :(得分:1)

这应该有效。

let string = ["1","2","3","4","5"]

let newString = "\"[\"" + string.joined(separator: "\",\"") + "\"]\""

print(newString) // Prints "["1","2","3","4","5"]"

编辑:最好的方法是使用@vadian的答案。虽然,这似乎不是有效的用例。

答案 2 :(得分:0)

extension Array {

    var fancyString: String {
        let formatted = self.description.replacingOccurrences(of: " ", with: "")
        return "\"\(formatted)\""
    }
}

let array = ["1", "2", "3", "4", "5"]
print(array.fancyString) //"["1","2","3","4","5"]"

注意:此解决方案可与任何类型的Array s一起使用

let ints = [1, 1, 3, 5, 8]
print(ints.fancyString) // "[1,1,3,5,8]"

答案 3 :(得分:0)

您可以使用此将数组转换为CSV字符串。

let productIDArray = ["1","2","3","4","5"]
let productIDString = productIDArray.joined(separator: ",")

答案 4 :(得分:0)

尝试使用此代码进行快速4

 override func viewDidLoad() {
        super.viewDidLoad()

        let strArray = self.getString(array: ["1", "2", "3"])
        print(strArray)

        // Do any additional setup after loading the view, typically from a nib.
    }

    func getString(array : [String]) -> String {
        let stringArray = array.map{ String($0) }
        return stringArray.joined(separator: ",")
    }

答案 5 :(得分:0)

func objectToJSONString(dictionaryOrArray: Any) -> String? {
do {

    //Convert to Data
    let jsonData = try JSONSerialization.data(withJSONObject: dictionaryOrArray, options: JSONSerialization.WritingOptions.prettyPrinted)

    //Convert back to string. Usually only do this for debugging
    if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
        return JSONString
    }

} catch {
    print(error.localizedDescription)
}

return nil
}

输出将为

Playground test

编辑准确答案

声明字符串的字符串扩展名

extension String {
var unescaped: String {
    let entities = ["\0": "\\0",
                    "\t": "\\t",
                    "\n": "\\n",
                    "\r": "\\r",
                    "\"": "\\\"",
                    "\'": "\\'",
                    ]

    return entities
        .reduce(self) { (string, entity) in
            string.replacingOccurrences(of: entity.value, with: entity.key)
        }
        .replacingOccurrences(of: "\\\\(?!\\\\)", with: "", options: .regularExpression)
        .replacingOccurrences(of: "\\\\", with: "\\")
}
}

修改后的代码将是

let array = ["1","2","3","4","5"]

let jsonString = objectToJSONString(dictionaryOrArray: array)

print(jsonString!.debugDescription) // this will print "[\"1\",\"2\",\"3\",\"4\",\"5\"]"

let result = jsonString!.debugDescription.unescaped

print(result)

所以输出

enter image description here

快乐编码:)

答案 6 :(得分:-1)

如果您希望从["1","2","3","4","5"]"["1","2","3","4","5"]",请选中

您可以通过简单的方式执行此操作,请尝试

 let arr = ["1","2","3","4","5"]
 let str = "\"\(arr)\""

enter image description here