Swift Program to Convert an Set of String to Comma Separated String

Swift provide a inbuilt method named as joined() to convert a set of string to comma separated string. This function return the concatenated elements of the given sequence by inserting the given separator in between each element of the sequence.


func joined(separator: sep) 

Where the separator parameter contains a string or sequence, which is further used to insert between each element of the given sequence. This function returns a concatenated or joined sequence of elements.


In the following code, we will create and initialize a set of strings. Then we join the elements of the set into a single string, which is separated by a comma using joined(separator: ”,”) function, and then display the output.

import Foundation
import Glibc

// Creating a set of string
var myFavfruit : Set = ["Orange", "Sweet apple", "Apple", "Kiwi", "Mango", "Jackfruit"] 

// Joining elements of the set by comma
let newString = myFavfruit.joined(separator: ",")

print("New String is: ", newString)


New String is:  Apple,Orange,Kiwi,Jackfruit,Sweet apple,Mango


Therefore, this is how we can convert a set of string to comma-separated string. The joined (separator:_) method easily convert a set in a string by concatenating the elements of a set. Apart from comma, use can also use any separator according to your preference.

Updated on: 06-Apr-2023


Kickstart Your Career

Get certified by completing the course

Get Started