cstringtokenizestrtok

Split string by a substring


I have following string:

char str[] = "A/USING=B)";

I want to split to get separate A and B values with /USING= as a delimiter

How can I do it? I known strtok() but it just split by one character as delimiter.


Solution

  • I known strtok() but it just split by one character as delimiter

    Nopes, it's not.

    As per the man page for strtok(), (emphasis mine)

    char *strtok(char *str, const char *delim);

    [...] The delim argument specifies a set of bytes that delimit the tokens in the parsed string. [...] A sequence of two or more contiguous delimiter bytes in the parsed string is considered to be a single delimiter. [...]

    So, it need not be "one character" as you've mentioned. You can using a string, like in your case "/USING=" as the delimiter to get the job done.