【问题标题】:Split the end of a string in C++ using string delimiter (,) [closed]使用字符串分隔符 (,) 在 C++ 中拆分字符串的结尾 [关闭]
【发布时间】:2021-03-30 04:08:31
【问题描述】:

我有以下格式的字符串:

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

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

【问题讨论】:

    标签: c++ string split


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!这行得通。
    猜你喜欢
    • 2018-06-09
    • 2016-07-05
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2015-06-29
    相关资源
    最近更新 更多