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

后端 未结 6 1785
名媛妹妹
名媛妹妹 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:30

    Don't use strtok() for this, since that's not what it's for.

    Use strchr() to find the first separator, and go from there:

    char a[] = "A,B,C,D,E";
    const char separator = ',';
    char * const sep_at = strchr(a, separator);
    if(sep_at != NULL)
    {
      *sep_at = '\0'; /* overwrite first separator, creating two strings. */
      printf("first part: '%s'\nsecond part: '%s'\n", a, sep_at + 1);
    }
    

提交回复
热议问题