C++字符串、宽字符串分割

整理C++字符串分割,包括C++11的和之前的用法。

#include 
#include 
#include 
#include 
using namespace std;

vector string_split(string srcStr, const string &delimit)
{
    int nPos = 0;
    vector vec;
    nPos = srcStr.find(delimit.c_str());
    while (-1 != nPos)
    {
        string temp = srcStr.substr(0, nPos);
        vec.push_back(temp);
        srcStr = srcStr.substr(nPos + delimit.length());
        nPos = srcStr.find(delimit.c_str());
    }
    vec.push_back(srcStr);
    return vec;
}

vector string_split11(const string &in, const string &delimit)
{
    vector ret;
    try {
        regex re{ delimit };
        return vector{std::sregex_token_iterator(in.begin(), in.end(), re, -1),
            std::sregex_token_iterator()};
    }
    catch (const std::exception e)
    {
        cout << __FUNCTION__<<" exception: " << e.what() << endl;
    }
    return ret;
}

vector charS_split(const char* in, const char* delimit)
{
    vector ret;
    int length = strnlen(in, 1024) + 1;
    char *tempIn = new char[length];

    strcpy_s(tempIn, length, in);
    char *next_token = NULL;
    char *token = NULL;
    token = strtok_s(tempIn, delimit, &next_token);

    while (token != NULL)
    {
        if (token != NULL)
        {
            ret.push_back(string(token));
            token = strtok_s(NULL, delimit, &next_token);
        }
    }
    delete tempIn;
    return ret;
}
vector charS_split11(const char* in, const char* delimit)
{
    vector ret;
    try {
        regex re{ delimit };
        return vector{std::cregex_token_iterator(in, in + strlen(in), re, -1),
            std::cregex_token_iterator()};
    }
    catch (const std::exception e)
    {
        cout << __FUNCTION__ << " exception: " << e.what() << endl;
    }
    return ret;
}

vector wstring_split(const wstring &in, const wstring &delimit)
{
    vector vec;
    wstring tempString = in;
    size_t nPos = in.find(delimit.c_str());
    while (wstring::npos != nPos)
    {
        wstring temp = tempString.substr(0, nPos);
        vec.push_back(temp);
        tempString = tempString.substr(nPos + delimit.length());
        nPos = tempString.find(delimit.c_str());
    }
    vec.push_back(tempString);

    return vec;
}

vector wstring_split11(const wstring &in, const wstring &delimit)
{
    vector ret;
    try
    {
        std::wregex rx{ delimit };
        return vector{std::wsregex_token_iterator(in.begin(), in.end(), rx, -1),
            std::wsregex_token_iterator()
        };
    }
    catch (const std::exception e)
    {
        cout << __FUNCTION__ << " exception: " << e.what() << endl;
    }
    return ret;
}
vector wcharS_split(const wchar_t* in, const wchar_t* delimit)
{
    vector ret;
    int length = wcsnlen_s(in, 1024) + 1;
    wchar_t *tempIn = new wchar_t[length];

    wcscpy_s(tempIn, length, in);

    wchar_t *next_token = NULL;
    wchar_t *token = NULL;
    token = wcstok_s(tempIn, delimit, &next_token);

    while (token != NULL)
    {
        if (token != NULL)
        {
            ret.push_back(wstring(token));
            token = wcstok_s(NULL, delimit, &next_token);
        }
    }
    delete tempIn;
    return ret;
}
vector wcharS_split11(const wchar_t *in, const wchar_t *delimit)
{
    vector ret;
    try
    {
        std::wregex re{ delimit };
        return vector{std::wcregex_token_iterator(in, in + wcslen(in), re, -1),
            std::wcregex_token_iterator()};
    }
    catch (const std::exception e)
    {
        cout << __FUNCTION__ << " exception: " << e.what() << endl;
    }
    return ret;
}

void PrintFunc (string iStr) {
  cout << iStr << endl;
}
int main(int argc, char *argv[])
{
    vector ret = string_split("hello__world!__I__am__OK!", "__");
    //string oneString;
    for_each (ret.begin(), ret.end(), PrintFunc);
    for_each (ret.begin(), ret.end(), [](const string& s){cout< wVec;
    wVec = wstring_split(L"Hello__world!__I__am__OK!", L",,");
    for_each(wVec.begin(), wVec.end(), [](const wstring &s){wcout<

 

你可能感兴趣的:(C++,字符串分割)