How to Convert a Swift Array to a String

How do I convert a Swift Array to a String?

If the array contains strings, you can use the String's join method:

var array = ["1", "2", "3"]

let stringRepresentation = "-".join(array) // "1-2-3"

In Swift 2:

var array = ["1", "2", "3"]

let stringRepresentation = array.joinWithSeparator("-") // "1-2-3"

This can be useful if you want to use a specific separator (hypen, blank, comma, etc).

Otherwise you can simply use the description property, which returns a string representation of the array:

let stringRepresentation = [1, 2, 3].description // "[1, 2, 3]"

Hint: any object implementing the Printable protocol has a description property. If you adopt that protocol in your own classes/structs, you make them print friendly as well

In Swift 3

  • join becomes joined, example [nil, "1", "2"].flatMap({$0}).joined()
  • joinWithSeparator becomes joined(separator:) (only available to Array of Strings)

In Swift 4

var array = ["1", "2", "3"]
array.joined(separator:"-")

How to convert String Array to String in Swift

This should work.

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

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

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

Edit: The best way is to use @vadian's answer. Albeit, this doesn't seem like a valid use case.

Swift: Turn array into String

You can do:

let string = array.map { String($0) }
.joined(separator: ", ")

The map call converts the array of numbers to an array of strings, and the joined combines them together into a single string with whatever separator you want between the individual strings.

Or, if this is to be presented in the UI the numbers could require either decimal points and/or thousands separators, then it's better to show the results in a localized format using NumberFormatter:

let array = [1.0, 1.1, 1.2, 1.3]
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 2

let string = array.compactMap { formatter.string(for: $0) }
.joined(separator: ", ")

Which, for US users, would result in:

1.00, 1.10, 1.20, 1.30

But for German users, it would result in:

1,00, 1,10, 1,20, 1,30

And if you wanted this to be presented nicely as "A, B, and C", in iOS 13+ and/or macOS 10.15+ you can use ListFormatter:

let array = [1.0, 1.1, 1.2, 1.3]
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 2

let strings = array.compactMap { formatter.string(for: $0) }
if let result = ListFormatter().string(from: strings) {
print(result)
}

Resulting in:

1.00, 1.10, 1.20, and 1.30

Convert Any array to String in swift 3

You should be casting your json object as so:

if let object = json as? [String:Any] {
...

How to convert the [AnyObject] Array to string in swift

First map the values to String values, and then just join them using separator that you like:

let description: String = ["A12", 1, 2, "Test"].map{ String(describing: $0) }.joined(separator: ", ")
print(description)

Convert ArrayString to String then back to ArrayString

The answer would be to convert the string using NSJSONSerialization

Thanks Vadian for that tip

let dataString = stringArray.dataUsingEncoding(NSUTF8StringEncoding)
let newArray = try! NSJSONSerialization.JSONObjectWithData(dataString!, options: []) as! Array<String>

for string in newArray {
print(string)
}

voila there you have it, it's now an array of strings

Swift Convert A String-As-An-Array, To An Array Of Strings

Encode your "string array" to data, then decode this data as JSON to a Swift Array.

Like this, for example:

let source = "[\"word1\",\"word2\"]"

guard let data = source.dataUsingEncoding(NSUTF8StringEncoding),
arrayOfStrings = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String] else {
fatalError()
}

print(arrayOfStrings) // ["word1", "word2"]
print(arrayOfStrings[1]) // "word2"


Related Topics



Leave a reply



Submit