strtok

来源:互联网 发布:win10 占用80端口 编辑:程序博客网 时间:2024/06/08 07:30

原型:char *strtok(char *s, char *delim);
功能:分解字符串。s为要分解的字符串,delim为分隔符字符串。
说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。 strtok在s中查找包含在delim中的字符并用NULL(‘\0’)来替换,直到找遍整个字符串。 返回指向下一个标记串。当没有标记串时则返回空字符NULL。

 /*    注意返回值      */#include <iostream>  #include "string.h"#include "stdio.h"using namespace std;  int main()  {      //时间格式 2010/08/11 10:38:22      char strEventTime[] = "2010/08/11 10:38:22";      char *token = NULL;      token = strtok(strEventTime, "/");      char *year = token;      if (token != NULL)      {          token = strtok(NULL, "/");      }      printf("%s\n",year);    printf("%s\n",token);    return 0;  }