C语言 带空格分隔符的 strtok

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4160273/
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:00:47  来源:igfitidea点击:

strtok with space delimiter

cstrtok

提问by Nadav Stern

hey i am trying to use the strtok function in C with " " as a delimiter and for some reason it does not work. can some one please tell me how to parse using strtok with a space as a delimiter thanks in advance

嘿,我试图在 C 中使用 strtok 函数,并以“”作为分隔符,但由于某种原因它不起作用。有人可以告诉我如何使用带有空格作为分隔符的 strtok 进行解析,提前致谢

回答by Michael Burr

Stolen (and slightly modified) from here.

这里被盗(并稍作修改)。

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ");
  }
  return 0;
}

回答by krishna

Use tab "\t" or use both " \t" ie. space and tab both...see if it works

使用制表符“\t”或同时使用“\t”即。空格和制表符都......看看它是否有效