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 plain copy
 
  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 plain copy
 
    1. Splitting string "- This, a sample string." into tokens:  
    2. This  
    3. a  
    4. sample  
    5. string  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM