cf_string_split_once¶
Category: string
GitHub: cute_string.h
Splits the string at the first occurrence of ch, returning the left half.
| Parameters | Description |
|---|---|
s |
The string. Must be a dynamically allocated string from this string API. Does not work on string literals. |
ch |
The delimiter character. |
Return Value¶
Returns the string to the left of ch. Returns NULL if ch isn't found (does not modify s).
Code Example¶
Splitting a path into components using a loop.
sdyna char* path = smake("/home/user/documents/file.txt");
sdyna char* part;
while ((part = ssplit_once(path, '/'))) {
printf("Part: %s\n", part);
sfree(part);
}
printf("Remaining: %s\n", path);
sfree(path);
Remarks¶
Shortform: ssplit_once(s, ch). After the call, s contains the string to the right of ch.
You must call sfree on the returned string.
This function is intended to be used in a loop, successively chopping off pieces of s.
A much easier, but slightly slower, version of this function is ssplit, which returns
an array of strings all at once.