通过多个字符串分隔符将字符串拆分为向量

时间:2021-12-17 21:37:54

In C++, without using any boost routines, we know how to:

在C ++中,不使用任何boost程序,我们知道如何:

  • split a string by a single char value or multiple char values

    通过单个char值或多个char值拆分字符串

  • split a string by a single string value:

    用单个字符串值拆分字符串:

    void ParseStringByStringSeparator(string s, const string separator, vector<string>& result)
    {
        result.clear();
        size_t pos = 0;
        string token;
    
        while ((pos = s.find(separator)) != string::npos) {
            token = s.substr(0, pos);
            result.push_back(token);
            s.erase(0, pos + separator.length());
        }
        result.push_back(s);
    }
    

But how to split a string by multiple string values?

但是如何用多个字符串值拆分字符串?

For example, If I have the following string value "Hello I am a String" and my separators are " " and "am" then I would like to obtain the following vector<string> value:

例如,如果我有以下字符串值“Hello我是一个字符串”并且我的分隔符是“”和“am”那么我想获得以下vector 值:

{"Hello","I","a","String"}

Any hint?

1 个解决方案

#1


0  

This function do what you want to do. It basically iterate on the vector of delimiters, applying your function for each one :

此功能可以执行您想要执行的操作。它基本上迭代分隔符的向量,为每个分隔符应用你的函数:

vector<string> SplitStringMultipleStrParameters(string s,const vector<string>& separators) {

    //The result to be returned
    vector<string> result = { s };

    //Iterate on the list of separators, so for each separators
    for (const auto& sep : separators) {
        //toReplaceBy will be the next vector of strings where it will iterate
        vector<string> toReplaceBy, tempRes;

        //For each strings, we will split on "sep", the separator
        for (auto&a : result) {
            //Because of the result vector being cleared every time your function get called
            //It get in a temp vector that we will concatenate after
            ParseStringByStringSeparator(a, sep, tempRes);
            //Concatenation of theses vectors
            toReplaceBy.insert(toReplaceBy.end(), tempRes.begin(), tempRes.end());
        }

        //Erasing all strings that are empty. C++11 code here required because of the lambda
        toReplaceBy.erase(std::remove_if(toReplaceBy.begin(), toReplaceBy.end(),
            [](const std::string& i) {
            return i == "";
        }), toReplaceBy.end());

        //The vector containing strings to be splited is replaced by the split result on this iteration
        result = toReplaceBy;
        //And we will split those results using the next separator, if there's more separator to iterate on
    }
    return result;
}

Code to test :

要测试的代码:

string test = "Hello I am a string";

auto r = SplitStringMultipleStrParameters(test, { " ", "am" });

for (auto& a : r) {
    std::cout << a << '\n';
}

It needs to be compiled with a C++11 compiler, and need to include the <algorithm> header.

它需要使用C ++ 11编译器进行编译,并且需要包含 标头。

If your compiler does not compile C++11 code, here is the function :

如果你的编译器没有编译C ++ 11代码,那么这里是函数:

vector<string> SplitStringMultipleStrParameters(string s,const vector<string>& separators) {
    vector<string> result = { s };
    for (const auto& sep : separators) {
        vector<string> toReplaceBy, tempRes;
        for (auto&a : result) {
            ParseStringByStringSeparator(a, sep, tempRes);
            toReplaceBy.insert(toReplaceBy.end(), tempRes.begin(), tempRes.end());
        }
        auto it = toReplaceBy.begin();
        while (it != toReplaceBy.end()) {
            if ((*it) == "")
                it = toReplaceBy.erase(it);
            else
                ++it;
        }
        result = toReplaceBy;
    }
    return result;
}

#1


0  

This function do what you want to do. It basically iterate on the vector of delimiters, applying your function for each one :

此功能可以执行您想要执行的操作。它基本上迭代分隔符的向量,为每个分隔符应用你的函数:

vector<string> SplitStringMultipleStrParameters(string s,const vector<string>& separators) {

    //The result to be returned
    vector<string> result = { s };

    //Iterate on the list of separators, so for each separators
    for (const auto& sep : separators) {
        //toReplaceBy will be the next vector of strings where it will iterate
        vector<string> toReplaceBy, tempRes;

        //For each strings, we will split on "sep", the separator
        for (auto&a : result) {
            //Because of the result vector being cleared every time your function get called
            //It get in a temp vector that we will concatenate after
            ParseStringByStringSeparator(a, sep, tempRes);
            //Concatenation of theses vectors
            toReplaceBy.insert(toReplaceBy.end(), tempRes.begin(), tempRes.end());
        }

        //Erasing all strings that are empty. C++11 code here required because of the lambda
        toReplaceBy.erase(std::remove_if(toReplaceBy.begin(), toReplaceBy.end(),
            [](const std::string& i) {
            return i == "";
        }), toReplaceBy.end());

        //The vector containing strings to be splited is replaced by the split result on this iteration
        result = toReplaceBy;
        //And we will split those results using the next separator, if there's more separator to iterate on
    }
    return result;
}

Code to test :

要测试的代码:

string test = "Hello I am a string";

auto r = SplitStringMultipleStrParameters(test, { " ", "am" });

for (auto& a : r) {
    std::cout << a << '\n';
}

It needs to be compiled with a C++11 compiler, and need to include the <algorithm> header.

它需要使用C ++ 11编译器进行编译,并且需要包含 标头。

If your compiler does not compile C++11 code, here is the function :

如果你的编译器没有编译C ++ 11代码,那么这里是函数:

vector<string> SplitStringMultipleStrParameters(string s,const vector<string>& separators) {
    vector<string> result = { s };
    for (const auto& sep : separators) {
        vector<string> toReplaceBy, tempRes;
        for (auto&a : result) {
            ParseStringByStringSeparator(a, sep, tempRes);
            toReplaceBy.insert(toReplaceBy.end(), tempRes.begin(), tempRes.end());
        }
        auto it = toReplaceBy.begin();
        while (it != toReplaceBy.end()) {
            if ((*it) == "")
                it = toReplaceBy.erase(it);
            else
                ++it;
        }
        result = toReplaceBy;
    }
    return result;
}