forEachSplit

inline fun CharSequence.forEachSplit(delimiter: Char, action: (segment: CharSequence) -> Unit)(source)

Executes the given action for each segment of the CharSequence split by the delimiter. This function avoids creating intermediate collections, making it more memory-efficient for large strings or frequent calls compared to String.split().forEach().

Behavior notes:

  • Empty input invokes no callbacks (produces zero segments).

  • Leading/trailing/consecutive delimiters produce empty segments accordingly.

  • The segment passed to action is a mutable, reused view over the original input. If you need to retain a segment after the callback returns, call segment.toString().

Parameters

delimiter

The character used to split the string.

action

The action to perform on each segment. The segment is provided as a CharSequence,