c++ 分解字符串
来源:互联网 发布:邮箱大师 mac 编辑:程序博客网 时间:2024/06/05 11:52
之前用java分解过字符串,貌似用的是正则式,意外发现c++里面也有strtok函数
1)库函数 #include<cstring>
2)作用 把字符串分解成一系列的记号token,记号是用分隔符分离出来的字符序列。例如把“i love you”按空格分解就可以得到“i" "love" "you"
3)原理 第一次strtok(char *arr,char *) token从第一个不是分隔符的字符开始标记,发现分隔符时,将其替换成'\0',将该记号后面的一个字符位置放到一个程序员不可见的静态变量中,返回一个当前位置的指针。第二次调用时,用NULL作第一个参数,NULL调用上次函数保存的位置,当没有剩余记号时,返回NULL;
4)注意 字符串到最后是被改变了的
5)程序
#include <iostream>
#include<cstring>
using namespace std;
void main()
{
char s[]="this is love for you and me";
char *token;
cout<<"string is"<<s<<endl;
token=strtok(s," ");
while(token!=NULL)
{
cout<<token<<"\n";
token=strtok(NULL," ");
}
cout<<"last :"<<s<<endl;
}
#include<cstring>
using namespace std;
void main()
{
char s[]="this is love for you and me";
char *token;
cout<<"string is"<<s<<endl;
token=strtok(s," ");
while(token!=NULL)
{
cout<<token<<"\n";
token=strtok(NULL," ");
}
cout<<"last :"<<s<<endl;
}
0 0
- c语言字符串分解
- ExtractStrings c++builder 字符串分解
- C语言 - 分解字符串函数分解英语句子
- 【c++】【2015华为笔试】分解字符串
- c/c++编程题 之 字符串分解
- 分解字符串
- 字符串分解
- 分解字符串
- 分解字符串
- 分解字符串
- 分解字符串
- 分解字符串
- 分解字符串
- 分解字符串
- 分解字符串
- 分解字符串
- [C/C++] 分解质因数
- 分解字符串方法
- FOS中断管理
- Java设计模式
- SQL编程实例:Access数据库,两张表的统计,count、sum聚合函数的使用,iif的使用,group by的使用
- Codeforces Round #244 (Div. 2)
- jquery改变元素属性值
- c++ 分解字符串
- uva ``Accordian'' Patience
- git下载源码时只有.git\objects\pack目录下的.pa
- 算法竞赛入门经典 5.4.4多少块土地
- zookeeper分布安装笔记
- Yii 日志与调试 debugtoolbar
- 重构机房收费系统前夕
- PHP学习记录2
- 图片列表页显示