join an array of characters into a string in swift

To join an array of characters into a string in Swift, you can use the init(_:) initializer of the String class, passing in the array of characters as a parameter. Here's an example:

main.swift
let charArray: [Character] = ["H", "e", "l", "l", "o"]
let myString = String(charArray)
print(myString) // Output: "Hello"
123 chars
4 lines

Alternatively, you can use the joined() method of the Sequence protocol, passing in an empty string as the separator, like this:

main.swift
let charArray: [Character] = ["H", "e", "l", "l", "o"]
let myString = charArray.joined(separator: "")
print(myString) // Output: "Hello"
137 chars
4 lines

Both methods produce the same output, which is a string containing the characters in the array.

gistlibby LogSnag