Split string by a substring

前端 未结 4 616
清酒与你
清酒与你 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:14

    See this. I got this when I searched for your question on google.

    In your case it will be:

    #include 
    #include 
    
    int main (int argc, char* argv [])
    {
        char theString [16] = "abcd/USING=efgh";
        char theCopy [16];
        char *token;
        strcpy (theCopy, theString);
        token = strtok (theCopy, "/USING=");
        while (token)
        {
            printf ("%s\n", token);
            token = strtok (NULL, "/USING=");
        }
    
        return 0;
    }
    

    This uses /USING= as the delimiter.

    The output of this was:

    abcd                                                                                                                                                                                                                      
    efgh 
    

    If you want to check, you can compile and run it online over here.

提交回复
热议问题