Skip to content

ssplit

Category: string
GitHub: cute_string.h


Splits a string about the character ch, scanning from left-to-right.

#define ssplit(s, ch) cf_string_split(s, ch)
Parameters Description
s The string.
ch A character to split about.

Return Value

Returns a dynamic array of all delimited strings (see dyna).

Code Example

Splitting a string about '.'.

sdyna char s = NULL;
sset(s, "split.here.in.a.loop");
const char splits_expected[] = {
    "split",
    "here",
    "in",
    "a",
    "loop",
};
dyna char array_of_splits = ssplit(s, '.');
for (int i = 0; i < alen(array_of_splits); ++i) {
    const char split = array_of_splits[i];
    CF_TEST_ASSERT(sequ(split, splits_expected[i]));
    sfree(split);
}
afree(array_of_splits);

Remarks

s is not modified. You must call sfree on the returned strings and afree on the returned array.

sdyna
ssplit_once