strtok c++里面,函数strtok怎么用
大家好,今天来为大家解答strtok这个问题的一些问题点,包括c++里面,函数strtok怎么用也一样很多人还不知道,因此呢,今天就来为大家分析分析,现在让我们一起来看看吧!如果解决了您的问题,还望您关注下本站哦,谢谢~
C语言中strtok用法
strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。如果要保持原字符串的完整,可以使用strchr和sscanf的组合等。
strtok是一个线程不安全的函数,因为它使用了静态分配的空间来存储被分割的字符串位置
线程安全的函数叫strtok_r,ca。
运用strtok来判断ip或者mac的时候务必要先用其他的方法判断'.'或':'的个数,因为用strtok截断的话,比如:"192..168.0...8..."这个字符串,strtok只会截取四次,中间的...无论多少都会被当作一个key。
函数strtok保存string中标记后面的下一个字符的指针,并返回当前标记的指针。
后面再调用strtok时,第一个参数为NULL,继续将string标记化。NULL参数表示调用strtok继续从string中上次调用 strtok时保存的位置开始标记化。
如果调用strtok时已经没有标记,则strtok返回NULL。注意strtok修改输入字符串,因此,如果调用strtok之后还要在程序中使用这个字符串,则应复制这个字符串。
C++ strtok函数
调用方式: char*strtok(char*str1, char*str2);
说明: strtok()函数的原型在string.h中
功能说明:函数strtok()返回字符串str1中指向一个由str2所指定的字符或者字符串的分隔符的指针,当没有要返回的分隔符时,就返回一个空指针。
函数strtok()实际上修改了有str1指向的字符串。每次找到一个分隔符后,一个空(NULL)就被放到分隔符处,函数用这种方法来连续查找该字符串。
例子:
#include<string.h>
#include<stdio.h>
int main()
{
char*p;
char str[100]="This is a test,and you can use it";
p= strtok(str,"");//注意,此时得到的 p为指向字符串:"This",即在第一个分隔符前面的字符串,即每次找到一个分隔符后,一个空(NULL)就被放到分隔符处,所以此时NULL指针指向后面的字符串:"is a test,and you can use it"。
printf("%s\n",p);//此时显示:This
do
{
p= strtok(NULL,",");// NULL即为上面返回的指针,即字符串:
//"is a test,and you can use it"。
if(p)
printf("|%s",p);
}while(p);
system("pause");
return 0;
}
strtok函数的用法详解
C库函数- strtok()
描述
C库函数 char*strtok(char*str, const char*delim)分解字符串 str为一组字符串,delim为分隔符。
声明
下面是 strtok()函数的声明。
char*strtok(char*str, const char*delim)参数
str--要被分解成一组小字符串的字符串。
delim--包含分隔符的 C字符串。
返回值
该函数返回被分解的第一个子字符串,如果没有可检索的字符串,则返回一个空指针。
实例
下面的实例演示了 strtok()函数的用法。
实例
#include
#include
int main(){
char str[80]=“This is- www.runoob.com- website”;
const char s[2]=“-”;
char*token;
/*获取第一个子字符串*/
token= strtok(str, s);
/*继续获取其他的子字符串*/
while( token!= NULL){
printf(“%s\n”, token);
token= strtok(NULL, s);
}
return(0);
}让我们编译并运行上面的程序,这将产生以下结果:
This is
www.runoob.com
website推荐:《C语言教程》
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<ioe@informatik.tu-chemnitz.de>
*- 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<jason@topic.com.au>,
* Matthew Hawkins<matt@mh.dropbear.id.au>
*- Kissed strtok() goodbye
*/
关于strtok和c++里面,函数strtok怎么用的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。