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

后端 未结 3 1925
星月不相逢
星月不相逢 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:15

    You can use char * strstr(const char *haystack, const char *needle) to locate your delimiter string within your string.

    char a[14] = "Hello,World!";
    char b[2] = ", ";
    char *start = a;
    char *delim;
    do {
        delim = strstr(start, b);
        // string between start and delim (or end of string if delim is NULL).
        start = delim + 2; // Use lengthof your b string.
    } while (delim);
    

提交回复
热议问题