Swift Array joined()

The joined() method returns a new string by concatenating all the elements in an array, separated by a specified separator.

Example

var message = ["Swift", "is","fun"]

// join all elements of array with space between them var newString = message.joined(separator:" ")
print(newString) // Output: Swift is fun

joined() Syntax

The syntax of the joined() method is:

array.joined(separator: delimiter)

Here, array is an object of the Array class.


joined() Parameter

The joined() method takes one parameter:

  • delimiter (optional) - the separator to join the elements

Note: If we call joined() without any parameter, the elements are joined without any separator.


joined() Return Value

  • returns a string with all the array elements joined by a separator

Example: Swift joined()

var brands = ["Dell", "HP", "Apple"]

// join elements with no separator var result1 = brands.joined()
// join elements with space between them var result2 = brands.joined(separator:" ")
// join elements with comma between them var result3 = brands.joined(separator:", ")
print(result1) print(result2) print(result3)

Output

DellHPApple
Dell HP Apple
Dell, HP, Apple

Here, we can see that the joined() method converts all the array elements into a string and separates each element by the specified separator.

Did you find this article helpful?