#31: Join a String Array in Swift 4+

To combine an array of strings into a single string, use joined(separator:):

let words = ["Swift", "Bites", "Rock"]
let sentence = words.joined(separator: " ")
// "Swift Bites Rock"

If you do not need a separator, use joined():

["a", "b", "c"].joined() // "abc"

This is simpler and more efficient than manually looping and appending into a result string.


I'm also on Twitter and GitHub.

Subscribe via RSS.