Skip to content

cf_string_split

Category: string
GitHub: cute_string.h


Splits the string at every occurrence of ch, returning a dynamic array of strings.

#define cf_string_split(s, ch) ssplit(s, ch)
Parameters Description
s The source C string (not modified).
ch The delimiter character.

Return Value

Returns a dyna char** array of newly allocated strings. Free each string with sfree, then free the array with afree.

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_ASSERT(sequ(split, splits_expected[i]));
    sfree(split);
}
afree(array_of_splits);
sfree(s);

Remarks

Shortform: ssplit(s, ch). The original string s is not modified. You must call sfree on each returned string and afree on the returned array.

cf_string_split_once
cf_string_find