Creates a new string containing the characters in the given sequence.
SDK
- Xcode 9.0+
Framework
- Swift Standard Library
Declaration
Parameters
characters
A string instance or another sequence of characters.
Discussion
You can use this initializer to create a new string from the result of one or more collection operations on a string’s characters. For example:
let str = "The rain in Spain stays mainly in the plain."
let vowels: Set<Character> = ["a", "e", "i", "o", "u"]
let disemvoweled = String(str.lazy.filter { !vowels.contains($0) })
print(disemvoweled)
// Prints "Th rn n Spn stys mnly n th pln."