C# · Splitting and Partitioning

Splitting and Partitioning

using StringZilla; 6.0.0-rc.1 · 91a27bdd

The iterators are lazy and allocation-free: each is a ref struct enumerator yielding zero-copy ReadOnlySpan<byte> views, so a foreach never touches the heap. The codepoint and segmentation iterators batch 64 boundaries per native call; the substring and byte-set splits and the match iterators advance one find at a time. Policies are fluent methods on the returned value.

foreach (Rune rune in Sz.EnumerateRunes(text)) Use(rune);   // cf. string.EnumerateRunes
foreach (var word in Sz.EnumerateWords(text)) Use(word);    // also Graphemes/Sentences/LineBreaks

foreach (var field in Sz.Split(line, ","u8)) Use(field); // cf. string.Split, but zero-copy
foreach (var part in Sz.RSplit(path, "/"u8).WithMaxSplit(1)) Use(part); // from the end, at most one split
foreach (var token in Sz.SplitAny(text, separators)) Use(token); // split on any byte in a Byteset
foreach (var line in Sz.SplitWhitespaces(text).SkipEmpty()) Use(line); // collapse whitespace runs

foreach (long at in Sz.EnumerateMatches(haystack, "ab"u8)) Use(at); // every offset; .Overlapping() for overlaps
foreach (var m in Sz.EnumerateUncasedMatches(haystack, "ß"u8)) Use(m); // caseless; m.Offset, m.Length

var (before, separator, after) = Sz.Partition(text, "="u8); // split at the first "="

WithMaxSplit(n) stops after n separators: 0 yields the whole text, and a negative n, the default, splits at every separator. An empty separator yields the whole text, a trailing separator leaves a final empty segment unless .SkipEmpty(), and an empty needle matches at every offset up to and including the end, or at every codepoint boundary when uncased.

API

StructSplitEnumerableAllocation-free foreach source over split segments. See Sz.Split.4
StringZilla.cs:895 ↗
struct SplitEnumerable
Skip*SkipEmpty× 2Drop zero-length segments.
StringZilla.cs:922 ↗
SplitEnumerable SkipSkipEmpty();
KeepSeparatorKeep the separator attached to each segment: the trailing separator when splitting forward, the leading separator when splitting in reverse.
StringZilla.cs:927 ↗
SplitEnumerable KeepSeparator();
WithMaxSplitStop after maxSplit separators, leaving the rest as one final segment: 0 yields the whole text, and a negative count, the default, splits at every separator.
StringZilla.cs:933 ↗
SplitEnumerable WithMaxSplit(long maxSplit);
GetEnumeratorReturns an enumerator over the split segments.
StringZilla.cs:937 ↗
SplitEnumerator GetEnumerator();
StructTokenSplitEnumerableAllocation-free foreach source over separator-token splits. See Sz.SplitWhitespaces.3
StringZilla.cs:1066 ↗
struct TokenSplitEnumerable
WithSeparatorsAlso yield the separator runs, interleaved with the gaps (lossless reconstruction).
StringZilla.cs:1080 ↗
TokenSplitEnumerable WithSeparators();
OnlySeparatorsYield only the separator runs.
StringZilla.cs:1083 ↗
TokenSplitEnumerable OnlySeparators();
GetEnumeratorReturns an enumerator over the separator-token split spans.
StringZilla.cs:1089 ↗
TokenSplitEnumerator GetEnumerator();