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 conforms to StringProtocol.

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: Bound, maxSplits: Int, omittingEmptySubsequences: Bool) -> [Range<Bound>]

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

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

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

func joined() -> FlattenSequence<Range<Bound>>

Returns the elements of this sequence of sequences, concatenated.

func joined<Separator>(separator: Separator) -> JoinedSequence<Range<Bound>>

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.