一個十分有用的標準C++寫的split(string) 函數

來源:互聯網
上載者:User
標準串的:/********************************************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);elsev.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;}// testint 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;}

 

 

引用:http://hi.baidu.com/ztianl/blog/item/122b46453062e43a8694737d.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.