`split_with_delimiter` String split including the delimiter

Many times I've been looking for way to split a string in such a way that the delimiter is part of the result, interleaved with the slices. For example:

let split = "Mary had a little lamb. The lamb lives at Mary's".split_with_delimiter('lamb').collect();
assert_eq!(split , ["Mary had a little ", "lamb", ". The ", "lamb", " lives at Mary's"]);

I've found split_inclusive which is not quite what I was looking for, as it has the delimiter stuck to the previous slice. However, split_inclusive has a similar justification for the inclusion in the API as split_with_delimiter has.

There are many stack overflow discussions about how to implement this in different languages and most of the time, hacky solutions. So, I am surprised that this is not already part of a standard library for string operations.

There are many possible combinations here, and unfortunately libstd instead of having optional arguments or some options struct, creates a new pair of functions (with reverse variant) for every possible tweak, so the string API is getting crowded.

Have you tried match_indices?

Using intersperse with nightly or from the itertools crate, for many use cases you could write:

let delim = "lamb";
let split = input.split(delim).intersperse(delim);

Of course, this isn't an option if the delimiter can match different strings, or if you wanted to use the substring pointer addresses for something.

2 Likes

A two-pass approach (can the compiler optimize this well?) is to .split_inclusive(pat).flat_map(.split_before(pat)).

1 Like