C/C++ strtok函数

一、头文件:#include <string.h>

 

二、函数原型:char * strtok (char *str, const char * delimiters);

 

三、参数:str,待分割的c风格的字符串(c-string);delimiters,分割符字符串。

 

 

 

四、说明:

将字符串str分解成若干个单词,单词之间以delimiters字符串中的任一一个字符分割。第一次调用strtok时,str应该是一个c风格的字符串(c-string),随后的调用中,  str应该是一个NULL指针。

 

 

五、例子:

 

[cpp] view plaincopy
 
  1. /* strtok example */  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4.   
  5. int main ()  
  6. {  
  7.   char str[] ="- This, a sample string.";  
  8.   char * pch;  
  9.   printf ("Splitting string \"%s\" into tokens:\n",str);  
  10.   pch = strtok (str," ,.-");  
  11.   while (pch != NULL)  
  12.   {  
  13.     printf ("%s\n",pch);  
  14.     pch = strtok (NULL, " ,.-");  
  15.   }  
  16.   return 0;  
  17. }  


Output:

 

 

[cpp] view plaincopy
 
    1. Splitting string "- This, a sample string." into tokens:  
    2. This  
    3. a  
    4. sample  
    5. string  
posted on 2015-03-02 17:01  寻步  阅读(4791)  评论(0编辑  收藏  举报