split()根据关键字符分割字符串 →_→还在码代码的马子耕←_←

来源:互联网 发布:电力数据通信网 编辑:程序博客网 时间:2024/05/05 02:59

=======================================================================

联系方式
qq:3110057340
邮箱:mazigeng@qq.com
新浪微博:还在码代码的马

=======================================================================


=============说明=============

/********************

 * 功能:根据给定的关键字符cKey值,来分割字符串str。

 *      将分好的字符串通过list列表outSL传出。

 *      outSL中为每一个分割好的子串,所有子串中均不保护cKey值。

 * 参数:outSL  存放子串的buff

 *      str    待分割的字符串     

 *      cKey   分割的关键字

 * 返回:无

 * ******************/

 

 

 

 

=============拿来就用函数 (ASCII=============

 

void splitA(list<string>& outSL, string str, char cKey)

{

    // 打入结尾岗哨

    str.push_back(cKey);

 

    int fpos = -1;

 

    do{

        int bpos = str.find(cKey,fpos+1);

        int n = bpos - fpos - 1;

 

        string strSub = str.substr(fpos+1, n);

        if(!strSub.empty())

            outSL.push_back(strSub);

 

        fpos = bpos;

    }while(fpos != (int)(str.length() - 1));

}

 

 

=============拿来就用函数 (UNICODE=============

 

 

void splitW(list<wstring>& outSL, wstring str, wchar_t cKey)

{

    // 打入结尾岗哨

    str.push_back(cKey);

 

    int fpos = -1;

 

    do{

        int bpos = str.find(cKey,fpos+1);

        int n = bpos - fpos - 1;

 

        wstring strSub = str.substr(fpos+1, n);

        if(!strSub.empty())

            outSL.push_back(strSub);

 

        fpos = bpos;

    }while(fpos != (int)(str.length() - 1));

}

 

 

=============用例=============

 

int main()

{

    wstring strTest = L"Hello world this is split function";

    list<wstring> retList;

 

split(retList, strTest, L'l'); // 以l为关键字

 

    while(retList.begin() != retList.end())

    {

        wcout << retList.front() << endl;

        retList.pop_front();

    }

    return 0;

}

 

输出结果:

He

o wor

d this is sp

it function

 

 

=============头文件=============

#include <list>

#include <string>

 

using std::list;

using std::string;

using std::wstring;

 

 

 


0 0