c++里面,函数strtok怎么用?

尤其是参数怎么处理?

  strtok:
  分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。
  功能:
  分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
  例如:strtok("abc,def,ghi",","),最后可以分割成为abc def ghi.尤其在点分十进制的IP中提取应用较多。
  函数使用:

  strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。如果要保持原字符串的完整,可以使用strchr和sscanf的组合等。
  c
  #include<string.h>
  #include<stdio.h>
  int main(void)
  {
  char input[16]="abc,d";
  char*p;
  /*strtok places a NULL terminator
  infront of the token,if found*/
  p=strtok(input,",");
  if(p)
  printf("%s\n",p);
  /*Asecond call to strtok using a NULL
  as the first parameter returns a pointer
  to the character following the token*/
  p=strtok(NULL,",");
  if(p)
  printf("%s\n",p);
  return0;
  }
  

  c++
  
  #include<iostream>
  #include<cstring>
  using namespace std;
  int main()
  {
  char sentence[]="This is a sentence with 7 tokens";
  cout << "The string to be tokenized is:\n" << sentence << "\n\nThe tokens are:\n\n";
  char *tokenPtr=strtok(sentence,"");
  while(tokenPtr!=NULL) {
  cout<<tokenPtr<<'\n';
  tokenPtr=strtok(NULL,"");
  }
  //cout << "After strtok,sentence=" << tokenPtr<<endl;return0;
  }
  

  函数第一次调用需设置两个参数。第一次分割的结果,返回串中第一个 ',' 之前的字符串,也就是上面的程序第一次输出abc。
  第二次调用该函数strtok(NULL,","),第一个参数设置为NULL。结果返回分割依据后面的字串,即第二次输出d。
  strtok是一个线程不安全的函数,因为它使用了静态分配的空间来存储被分割的字符串位置
  线程安全的函数叫strtok_r,ca
  运用strtok来判断ip或者mac的时候务必要先用其他的方法判断'.'或':'的个数,因为用strtok截断的话,比如:"192..168.0...8..."这个字符串,strtok只会截取四次,中间的...无论多少都会被当作一个key
  其他相关信息
  下面的说明摘自于最新的Linux内核2.6.29,说明了这个函数已经不再使用,由速度更快的strsep()代替
  /** linux/lib/string.c** Copyright (C) 1991, 1992 Linus Torvalds*//** stupid library routines.. The optimized versions should generally be found
  * as inline code in <asm-xx/string.h> 
  * These are buggy as well.. 
  * * Fri Jun 25 1999, Ingo Oeser <[email protected]> 
  * - Added strsep() which will replace strtok() soon (because strsep() is 
  * reentrant and should be faster). Use only strsep() in new code, please. 
  ** * Sat Feb 09 2002, Jason Thomas <[email protected]>, 
  * Matthew Hawkins <[email protected]> 
  * - Kissed strtok() goodbye
  */
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-26
函数strtok将字符串分解为一系列标记(token)标记就是一系列用分隔符(delimiting chracter,通常是空格或标点符号)分开的字符。例如,在一行文本中,每个单词可以作为标记,空格是分隔符。
需要多次调用strtok才能将字符串分解为标记(假设字符串中包含多个标记)。第一次调用strtok包含两个参数,即要标记化的字符串和包含用来分隔标记的字符的字符串(即分隔符):在图5.33的例子中,下列语句:
tokenPtr = Strtok(string, " ");
将tokenPtr赋给string中第一个标记的指针。strtok的第二个参数””表示string中的标记用空格分开。
函数strtok搜索string中不是分隔符(空格)的第一个字符,这是第一个标记的开头。然后函数寻找字符串中的下一个分隔符,将其换成null(, w,)字符,这是当前标记的终点。函数strtok保存string中标记后面的下一个字符的指针,并返回当前标记的指针。
/* 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;
}

Output:

Splitting string "- This, a sample string." into tokens:
This
a
sample
string
后面再调用strtok时,第一个参数为NULL,继续将string标记化。NULL参数表示调用strtok继续从string中上次调用 strtok时保存的位置开始标记化。如果调用strtok时已经没有标记,则strtok返回NULL。图5.33的程序用strtok将字符串” This is sentence with 7 tokens”标记化。分别打印每个标记。注意strtok修改输入字符串,因此,如果调用strtok之后还要在程序中使用这个字符串,则应复制这个字 符串。
第2个回答  推荐于2017-09-16
char *strtok(char *s, char *delim);
分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。
strtok在s中查找包含在delim中的字符并用NULL('')来替换,直到找遍整个字符串。本回答被提问者采纳
第3个回答  2010-04-10
char * strtok ( char * str, const char * delimiters );

Parameters
str
C string to truncate. The contents of this string are modified and broken into smaller strings (tokens).
Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
delimiters
C string containing the delimiters.
These may vary from one call to another.
Return Value
A pointer to the last token found in string.
A null pointer is returned if there are no tokens left to retrieve.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/* 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;
}

Output:

Splitting string "- This, a sample string." into tokens:
This
a
sample
string

参考资料:http://www.cplusplus.com/reference/clibrary/cstring/strtok/

相似回答