The joined()
method returns a new string with the given elements joined with the specified delimiter.
Example
var str = ["I", "love","Swift"]
// join strings with space between them
var joinedStr = str.joined(separator:" ")
print(joinedStr)
// Output: I love Swift
joined() Syntax
The syntax of joined()
is:
string.joined(separator: delimiter)
Here, string is an object of the String
class.
joined() Parameter
The joined()
method takes one parameter:
- delimiter - the delimiter to be joined with the elements
Note: If we call joined()
without any parameter, the strings are joined without any separator.
joined() Return Value
- returns the joined string
Example: Swift joined()
var str = ["Swift", "Java", "JavaScript"]
// join strings with no separator
var join1 = str.joined()
// join strings with space between them
var join2 = str.joined(separator:" ")
// join strings with comma between them
var join3 = str.joined(separator:", ")
print(join1)
print(join2)
print(join3)
Output
SwiftJavaJavaScript Swift Java JavaScript Swift, Java, JavaScript