C language: How to get the remaining string after using strtok() once

后端 未结 6 1792
名媛妹妹
名媛妹妹 2021-02-13 16:42

My string is \"A,B,C,D,E\"
And the separator is \",\"
How can I get the remaining string after doing strtok() once, that is \"B,C,D,E\"

char a[] = \"A,B,         


        
6条回答
  •  情话喂你
    2021-02-13 17:23

    Try this:

    char a[] = "A,B,C,D,E";
    char * end_of_a = a + strlen(a); /* Memorise the end of s. */
    char * separator = ",";
    char * b = strtok(a, separator);
    printf("a: %s\n", a);
    printf("b: %s\n", b);
    
    /* There might be some more tokenising here, assigning its result to b. */
    
    if (NULL != b)
    {
      b = strtok(NULL, separator);
    }
    
    if (NULL != b)
    { /* Get reference to and print remainder: */
      char * end_of_b = b + strlen(b);
    
      if (end_of_b != end_of_a) /* Test whether there really needs to be something, 
                            will say tokenising did not already reached the end of a, 
                            which however is not the case for this example.  */
      {
        char * remainder = end_of_b + 1;
        printf("remainder: `%s`\n", remainder);
      }   
    }
    

提交回复
热议问题