trimming(while:)

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

Trim.swift:113
func trimming(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 trimming(while:) to get a substring without the white space at the beginning and end of the string:

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