使用字符串分隔符 (,) 在 C++ 中拆分字符串的结尾

时间:2020-12-19 22:25:55

标签: c++ string split

我有以下格式的字符串:

str1 = "a, b, c, d, e";
str2 = "aa, ba, ca, da, essd";
str2 = "aass, bsda, cads, dsda, esssdsd";

我想在拆分后提取字符串的结尾 --> e, essd, esssdsd

1 个答案:

答案 0 :(得分:1)

假设这些是 std::string,我会使用 rfind 找到最后一次出现的分隔符,然后从那里取出一个子字符串。例如:

size_t index = str.rfind(", ");
string last_element = str.substr(index + 2); // 2 is the size of the delimiter