zoukankan      html  css  js  c++  java
  • 这个split 不错 我喜欢的

    // stdLibStringSplit.cpp : 定义控制台应用程序的入口点。
    //


    #include "stdafx.h"
    /********************************************


    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\r\ncde\r\nfg,,\r\n " ;
    string tok = "\r\n";


    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;
    }
  • 相关阅读:
    HTML_严格模式与混杂模式
    不要和一种编程语言厮守终生:为工作正确选择(转)
    iOS开发编码建议与编程经验(转)
    UTF-8 和 GBK 的 NSString 相互转化的方法
    UICollectionView 总结
    UIViewController的生命周期及iOS程序执行顺序
    objective-c 中随机数的用法
    clipsToBounds 与 masksToBounds 的区别与联系
    网络请求 代码 系统自带类源码
    iOS CGRectGetMaxX/Y 使用
  • 原文地址:https://www.cnblogs.com/eaglezzb/p/4176499.html
Copyright © 2011-2022 走看看