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

时间:2017-07-26 16:04:28

标签: c++ string split

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

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

  • 将字符串拆分为单个字符串值:

    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);
    }
    

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

例如,如果我有以下字符串值"Hello I am a String"且我的分隔符为" ""am",那么我想获得以下vector<string>值:

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

任何提示?

1 个答案:

答案 0 :(得分:0)

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

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;
}

要测试的代码:

string test = "Hello I am a string";

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

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

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

如果你的编译器没有编译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;
}