Split string by a substring

前端 未结 4 713
清酒与你
清酒与你 2021-01-19 16:48

I have following string:

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

I want to split to get separate A and B values with /

4条回答
  •  無奈伤痛
    2021-01-19 17:10

    Here is a little function to do this. It works exactly like strtok_r except that the delimiter is taken as a delimiting string, not a list of delimiting characters.

    char *strtokstr_r(char *s, char *delim, char **save_ptr)
    {
        char *end;
        if (s == NULL)
            s = *save_ptr;
    
        if (s == NULL || *s == '\0')
        {
            *save_ptr = s;
            return NULL;
        }
    
        // Skip leading delimiters.
        while (strstr(s,delim)==s) s+=strlen(delim);
        if (*s == '\0')
        {
            *save_ptr = s;
            return NULL;
        }
    
        // Find the end of the token.
        end = strstr (s, delim);
        if (end == NULL)
        {
            *save_ptr = s + strlen(s);
            return s;
        }
    
        // Terminate the token and make *SAVE_PTR point past it.
        memset(end, 0, strlen(delim));
        *save_ptr = end + strlen(delim);
        return s;
    }
    

提交回复
热议问题