enumerateSubstrings(in:options:_:)
Enumerates the substrings of the specified type in the specified range of the string.
Declaration
func enumerateSubstrings<R>(in range: R, options opts: String.EnumerationOptions = [], _ body: @escaping (String?, Range<Self.Index>, Range<Self.Index>, inout Bool) -> Void) where R : RangeExpression, R.Bound == String.IndexParameters
- range:
The range within the string to enumerate substrings.
- opts:
Options specifying types of substrings and enumeration styles. If
optsis omitted or empty,bodyis called a single time with the range of the string specified byrange. - body:
The closure executed for each substring in the enumeration. The closure takes four arguments:
The enumerated substring. If
substringNotRequiredis included inopts, this parameter isnilfor every execution of the closure.The range of the enumerated substring in the string that
enumerate(in:options:_:)was called on.The range that includes the substring as well as any separator or filler characters that follow. For instance, for lines,
enclosingRangecontains the line terminators. The enclosing range for the first string enumerated also contains any characters that occur before the string. Consecutive enclosing ranges are guaranteed not to overlap, and every single character in the enumerated range is included in one and only one enclosing range.An
inoutBoolean value that the closure can use to stop the enumeration by settingstop = true.
Discussion
Mutation of a string value while enumerating its substrings is not supported. If you need to mutate a string from within body, convert your string to an NSMutableString instance and then call the enumerateSubstrings(in:options:using:) method.