split函数。
标准串的:
/********************************************
the tokenize function for std::string
*********************************************/
#include <string>
#include <vector>
#include <iostream>
using namespace std;
typedef basic_string<char>::size_type S_T;
static const S_T npos = -1;
////trim指示是否保留空串,默认为保留。
vector<string> tokenize(const string& src, string tok,
bool trim=false, string null_subst="")
{
if( src.empty() || tok.empty() ) throw "tokenize: empty
string\0";
vector<string> v;
S_T pre_index = 0, index = 0, len = 0;
while( (index = src.find_first_of(tok, pre_index)) !=
npos )
{
if( (len = index-pre_index)!=0 )
v.push_back(src.substr(pre_index, len));
else if(trim==false)
v.push_back(null_subst);
pre_index = index+1;
}
string endstr = src.substr(pre_index);
if( trim==false ) v.push_back( endstr.empty()?
null_subst:endstr );
else if( !endstr.empty() ) v.push_back(endstr);
return v;
}
////使用一个完整的串delimit(而不是其中的
某个字符)来分割src串,没有trim选项,即严格分割。
vector<string> split(const string& src, string delimit,
string null_subst="")
{
if( src.empty() || delimit.empty() ) throw "split:
empty string\0";
vector<string> v;
S_T deli_len = delimit.size();
long index = npos, last_search_position = 0;
while( (index=src.find(delimit,
last_search_position))!=npos )
{
if(index==last_search_position)
v.push_back(null_subst);
else
v.push_back( src.substr(last_search_position, index-
last_search_position) );
last_search_position = index + deli_len;
}
string last_one = src.substr(last_search_position);
v.push_back( last_one.empty()? null_subst:last_one );
return v;
}
//test
int main(void)
{
string src = ",ab,cde;,,fg,," ;
string tok = ",;" ;
vector<string> v1 = tokenize(src, tok ,true);
vector<string> v2 = tokenize(src, tok ,false,
"<null>");
cout<<"-------------v1:"<<endl;
for(int i=0; i<v1.size();i++)
{
cout<<v1[i].c_str()<<endl;
}
cout<<"-------------v2:"<<endl;
for(int j=0; j<v2.size();j++)
{
cout<<v2[j].c_str()<<endl;
}
try{
string s = "######123#4###56########789###";
string del = "";//"###";
vector<string> v3 = split(s, del, "<null>");
cout<<"-------------v3:"<<endl;
for(int k=0; k<v3.size();k++)
{
cout<<v3[k].c_str()<<endl;
}
}
catch (char *s) {
cout<<s<<endl;
}
return 0;
}
分享到:
相关推荐
自己封装的纯C++的字符串处理函数大全,像特别好用的 字符串切分 Split函数代码均已经过测试,并有接口说明,可方便调用。
第10章 Visual C++ 2010 MFC动态函数链接库 434 10.1 动态函数链接库简介 434 10.1.1 什么是动态函数链接库 434 10.1.2 动态函数链接库的优点 435 10.1.3 动态函数链接库的起源 436 10.1.4 动态函数链接库的原理 436...
第10章 VisualC++2010MFC动态函数链接库 第11章 VisualC++2010MFCActiveX控件 第12章 VisualC++2010MFC文件与注册表操作 第13章 VisualC++2010MFC数据库开发 第14章 VisualC++2010MFC多线程程序设计 第...
第10章 VisualC++2010MFC动态函数链接库 第11章 VisualC++2010MFCActiveX控件 第12章 VisualC++2010MFC文件与注册表操作 第13章 VisualC++2010MFC数据库开发 第14章 VisualC++2010MFC多线程程序设计 第...
Tcl的C函数库拥有清晰的接口而且便于使用。该函数库实现了基本的解释器,它有一套实现变量、流程控制和过程的核心脚本命令,而且还有一组用来存取操作系统服务以运行其他程序、存取文件系统和使用网络套接字的命令。...
如果想要强行转换变量类型,可以使用与C语言相同的函数settype()。 2.5 变量与常量 可能你已经注意到,变量都有一个美元符号($)的前缀。所有变量都是局部变量,为了使得定义的函数中可以使用外部变量,使用...
12.5 在/etc/passwd文件中使用NISisms 232 12.6 使用网络组 232 12.7 解决问题的一些技巧 233 12.8 小结 233 第13章 网络文件系统 235 13.1 NFS安装 235 13.2 启动和停止NFS守护程序 236 13.3 NFS状态 236 13.4 配置...
在 SQL Server 2005 中使用表值函数来实现空间数据库 SQL Server 2005的30个最重要特点 同时安装sql2000和sql2005的经验 类如何与界面绑定 在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息 使用Relations建立...
7.5.1 使用Visual C++开发动态链接库 -- DllForPb9 7.5.2 使用Delphi开发动态链接库 -- SetScreen 7.5.3 使用Visual Basic开发ActiveX动态链接库 -- CPUInfo 第8章 pipeline 数据管道 -- pipeline 第9章 DDE...