c++中字符串分割函数
来源:互联网 发布:sai for mac 10.12 编辑:程序博客网 时间:2024/05/22 17:34
1,简单点的方法以空格分割字符串
#include <iostream>
#include <string>
#include <sstream>
using
namespace
std;
void
main()
{
string str1 =
"qwe bbb 333"
;
string str2,str3,str4;
istringstream is(str1);
is>>str2>>str3>>str4;
cout<<str2<<
","
<<str3<<
","
<<str4<<endl;
}
2,具有通用性可以是任何字符分割的字符串
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <sstream>
#include <deque>
using namespace std;
//字符串分割函数
vector< string> split( string str, string pattern)
{
vector<string> ret;
if(pattern.empty()) return ret;
size_t start=0,index=str.find_first_of(pattern,0);
while(index!=str.npos)
{
if(start!=index)
ret.push_back(str.substr(start,index-start));
start=index+1;
index=str.find_first_of(pattern,start);
}
if(!str.substr(start).empty())
ret.push_back(str.substr(start));
return ret;
}
int main()
{
string str="1^1^^^22334455^^^**";
string pattern="^*";
vector< string> result=split(str,pattern);
cout<<"The result:"<< endl;
for(int i=0; i<result.size(); i++)
{
cout<<result[i]<< endl;
}
// cout<<"azhu"<<endl;
return 0;
}
- 标准C中strtok函数分割字符串
- 标准C中strtok函数分割字符串
- c中字符串分割函数strtok
- c 字符串分割函数
- 分割字符串函数split(C#)
- 分割字符串函数split(C#)
- c 分割字符串函数 split
- C语言字符串分割函数
- C语言中strtok函数进行分割字符串!
- C语言中strtok函数进行分割字符串!
- c++中字符串分割函数
- C strtok C中字符串分割
- 两个常用C字符串分割函数
- C/C++分割字符串:strtok函数
- C中的一个字符串分割函数
- C语言分割字符串函数strtok
- C语言中的字符串分割函数
- C语言分割字符串strtok函数
- 防攻击进行sha加密
- 9web xml元素
- Android动画原理分析
- webservice学习之处理Map等CXF无法自动转化的类型
- 提高项目31.1-统计代码中的if、while、 for字符串
- c++中字符串分割函数
- Commons_FileUpload
- webview
- Ubuntu 修改用户名和密码之后,进入不了图形界面
- Android之监听手机软键盘弹起与关闭
- 怎么区分静态网页和动态网页
- oracle 基础四 闪回
- 静态成员变量和非静态成员变量的对比
- numpy module 3 -- 科学计算1