Instance Method

joined(separator:)

Returns a new string by concatenating the elements of the sequence, adding the given separator between each element.

Declaration

func joined(separator: String = "") -> String
Available when Element is String.

Parameters

separator

A string to insert between each of the elements in this sequence. The default separator is an empty string.

Return Value

A single, concatenated string.

Discussion

The following example shows how an array of strings can be joined to a single, comma-separated string:

let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let list = cast.joined(separator: ", ")
print(list)
// Prints "Vivien, Marlon, Kim, Karl"

See Also

Splitting and Joining Elements

func split(separator: Element, maxSplits: Int, omittingEmptySubsequences: Bool) -> [ArraySlice<Element>]

Returns the longest possible subsequences of the collection, in order, around elements equal to the given element.

func split(maxSplits: Int, omittingEmptySubsequences: Bool, whereSeparator: (Element) -> Bool) -> [ArraySlice<Element>]

Returns the longest possible subsequences of the collection, in order, that don’t contain elements satisfying the given predicate.

func joined() -> FlattenSequence<Array<Element>>

Returns the elements of this sequence of sequences, concatenated.

func joined<Separator>(separator: Separator) -> JoinedSequence<Array<Element>>

Returns the concatenated elements of this sequence of sequences, inserting the given separator between each element.

func joined(separator: String) -> String

Returns a new string by concatenating the elements of the sequence, adding the given separator between each element.