
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Set of Strings to Comma-Separated String in Swift
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.
Syntax
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.
Example
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)
Output
New String is: Apple,Orange,Kiwi,Jackfruit,Sweet apple,Mango
Conclusion
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.