trimmingPrefix(while:)

Returns a SubSequence formed by discarding all elements at the start of the collection which satisfy the given predicate.

Trim.swift:31
func trimmingPrefix(while predicate: (Element) throws -> Bool) rethrows -> SubSequence

Parameters

predicate

A closure which determines if the element should be omitted from the resulting slice.

This example uses trimmingPrefix(while:) to get a substring without the white space at the beginning of the string:

let myString = "  hello, world  "
print(myString.trimmingPrefix(while: \.isWhitespace)) // "hello, world  "