函数原型:char *strtok(char *s, const char *delim);
Function:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
Description:strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串,当strtok()在参数s的字符串中发现到参数delim的分割字符时 则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回被分割出片段的指针。
看到这篇文章,感觉受益匪浅。
http://blog.csdn.net/helpxs/article/details/6958975
http://blog.csdn.net/helpxs/article/details/6959057
#include <iostream>
using namespace std;
int main()
{
char s[] = "192..168.0...8...";
const char* split="8.";
char *p = strtok(s,split);
while(p!=NULL)
{
cout<<p<<endl;
cout<<"another time"<<endl;//一共调用了4次
/*
* 因为用strtok截断的话,比如:"192..168.0...8..."这个字符串
strtok只会截取四次,中间的...无论多少都会被当作一个key
*/
p = strtok(NULL,split);
}
system("pause");
return 0;
}