C++写的split(string) 函数
来源:互联网 发布:js结果放入html 编辑:程序博客网 时间:2024/06/05 03:22
source: http://guoxinmiao8.blog.sohu.com/102130312.html
一个十分有用的标准C++写的split(string) 函数
- /********************************************
- 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选项,即严格分割。
view plainprint?
- 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
view plainprint?
- 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(string) 函数
- 自己写的一个C++ string split函数
- java的String.split函数
- std::string的split函数
- c++写的split函数
- 写的一个split函数
- 一个十分有用的标准C++写的split(string) 函数
- java string.split()函数的c/c++两种实现——strtok_r及string.find()
- String的split()函数的用法总结
- C++中string的分割函数split
- java String.split()函数的用法分析
- java中string 的split函数
- Java 中String的split函数简介
- java String.split()函数的用法分析
- java String.split()函数的用法分析
- java String.split()函数的用法分析
- java String.split()函数的用法分析
- java String.split()函数的用法分析
- target is null for setProperty的问题总结
- Yii使用CPagination实现同一页面多翻页
- 通用获取父节点/子节点/子节点下所有节点ID的存储过程
- php 实现 过滤国家ip访问功能
- js根据当前月取得历史或未来月份集合
- C++写的split(string) 函数
- PKU 2299 求解逆序数(使用归并或者树状数组) 树状数组及入门知识
- jquery.pagination.js分页
- linux下制作二进制bin 文件制做方法
- 阿里巴巴架构调整解读:淘宝C2C成为基础层
- 自定义表单中计算控件的插件代码
- JavaScript导出txt文件
- MySQL如何传输二进制日志
- oracle约束语句