Contents

init(_:)

Creates a string corresponding to the given collection of Unicode scalars.

Declaration

init(_ unicodeScalars: String.UnicodeScalarView)

Parameters

  • unicodeScalars:

    A collection of Unicode scalar values.

Discussion

You can use this initializer to create a new string from a slice of another string’s unicodeScalars view.

let picnicGuest = "Deserving porcupine"
if let i = picnicGuest.unicodeScalars.firstIndex(of: " ") {
    let adjective = String(picnicGuest.unicodeScalars[..<i])
    print(adjective)
}
// Prints "Deserving"

The adjective constant is created by calling this initializer with a slice of the picnicGuest.unicodeScalars view.

See Also

Working with String Views