Is there a way to split a string on multiple characters in C?

后端 未结 3 1923
星月不相逢
星月不相逢 2020-12-11 15:52

Is there a way in C to split a string (using strtok or any other way) where the delimiter is more than one character in length? I\'m looking for something like

3条回答
  •  一向
    一向 (楼主)
    2020-12-11 16:35

    Something like this maybe? No guarantees that this compiles. ;)

    char* strstrtok(char *haystack, char *needle) {
        static char *remaining = null;
        char *working;
    
        if(haystack)
             working = haystack;
        else if(remaining)
             working = remaining;
        else
             return NULL;
    
        char *result = working;
        if(result = strstr(working, needle))
            remaining = working + strlen(needle) + 1;
    
        return result;
    }
    

提交回复
热议问题