C语言:使用strtok()一次后如何获取剩余的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19724450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:51:49  来源:igfitidea点击:

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

cstrtok

提问by Eric Tseng

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"

我的字符串是 "A,B,C,D,E"
而分隔符是 ","
我怎样才能在做一次 strtok() 后得到剩余的字符串,即 "B,C,D,E"

char a[] = "A,B,C,D,E";
char * separator = ",";
char * b = strtok(a,separator);
printf("a: %s\n", a);
printf("b: %s\n", b);

The output is:
a: A
b: A

输出为:
a:A
b:A

But how to get the result
a: B,C,D,E
b: A

Thanks.

但是如何得到结果
a: B,C,D,E
b: A

谢谢。

回答by Neil

You can vary the set of delimiters, so simply pass an empty string:

您可以更改分隔符集,因此只需传递一个空字符串:

char a[] = "A,B,C,D,E";
char * separator = ",";
char * b = strtok(a, separator);
printf("b: %s\n", b);
char * c = strtok(NULL, "");
printf("c: %s\n", c);

回答by unwind

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

不要strtok()用于此,因为那不是它的用途。

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

使用strchr()查找第一个分离器,并从那里:

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

回答by alk

Try this:

尝试这个:

char a[] = "A,B,C,D,E";
const char *separator = ",";
char *b = strtok(a, separator);
while (b) {
    printf("element: %s\n", b);
    b = strtok(NULL, separator);
}

回答by Atle

strtokremembers the last string it worked with and where it ended. To get the next string, call it again with NULLas first argument.

strtok记住它使用的最后一个字符串以及它在哪里结束。要获取下一个字符串,请使用NULL第一个参数再次调用它。

char a[] = "A,B,C,D,E";
char *sep = strchr(a, ',');
*sep = '##代码##';
puts(a);
puts(sep + 1);

Note: This is not thread safe.

注意:这不是线程安全的。

回答by Fred Foo

If using strtokis not a requirement, you can use strchrinstead since the separator is a single character:

如果 usingstrtok不是必需的,您可以使用,strchr因为分隔符是单个字符:

##代码##

回答by nav_jan

printf("a: %s\n", a+1+strlen(b));

printf("a: %s\n", a+1+strlen(b));

Try this

尝试这个