How to Create a Comma-Separated String from an Array in Swift

In this article, I'll explain how to create a comma-separated string from an array in Swift.

Create a Comma-Separated String from a String Array

In Swift, to create a comma-separated string from a string array, you can use the joined(separator:) function.

array.joined(separator:)

The joined(separator:) function can be used when the elements of the array are of type String. It returns a new string by concatenating the elements separated by the specified separator.


Let's use the joined(separator:) function to generate a comma-separated string from a string array.

For example, to create a comma-separated string from the elements of the names array, you can write the following code:

let names = ["Smith", "Johnson", "Williams", "Brown"]

let csv = names.joined(separator: ",")
print(csv)

The output will look like this, showing that the comma-separated string is stored in csv:

Smith,Johnson,Williams,Brown

Swift - How to create a comma-separated string from an array 1


Create a Comma-Separated String from a Number Array

To create a comma-separated string from a number array in Swift, you first need to convert the numbers into strings.

You can use the map function to transform the elements into another type.


For example, to create a comma-separated string from an integer array called ids, you can do the following:

let ids = [15, 6, 7, 12, 8, 1]
let idsString = ids.map { String($0) }

let csv = idsString.joined(separator: ",")
print(csv)

In line 2, we use the map function to convert each element into a string and assign the result to idsString.

Then, we use the joined function to generate a comma-separated string from the string array.

The output will be:

15,6,7,12,8,1

Swift - How to create a comma-separated string from an array 2


Create a Comma-Separated String from an Object Array's Properties

Finally, let's create a comma-separated string from the properties of objects in an array.


Suppose we have an array of Student structs, and we want to generate a comma-separated string like firstName,lastName,age. Here's how:

struct Student {
    var lastName: String
    var firstName: String
    var age: Int
}

let students = [
    Student(lastName: "Smith", firstName: "Mia", age: 10),
    Student(lastName: "Johnson", firstName: "James", age: 9),
    Student(lastName: "Williams", firstName: "Henry", age: 8),
    Student(lastName: "Brown", firstName: "Oliver", age: 10),
    Student(lastName: "Smith", firstName: "Sophia", age: 11)
]

let studentCSV = students.map { "\($0.firstName),\($0.lastName),\($0.age)" }
let csv = studentCSV.joined(separator: "\n")
print(csv)

In line 15, we use the map function to generate a new array studentCSV where each element is a string containing firstName,lastName,age.

In line 16, we use the joined function with "\n" as the separator to concatenate them with line breaks.

The output will be:

Mia,Smith,10
James,Johnson,9
Henry,Williams,8
Oliver,Brown,10
Sophia,Smith,11

Swift - How to create a comma-separated string from an array 3


That's how you can create a comma-separated string from an array in Swift.