如何获取用"::"分隔的标记化字符串中的最后一个元素C++?

How to get last element in tokenized string in C++ separated by "::"?

本文关键字:字符串 C++ 元素 最后一个 分隔 何获取 获取      更新时间:2023-10-16

我正在开发C++,

我有一个字符串如下:

string str = "rake::may.chipola::ninbn::myFuntion";

如何从上面的字符串中获取最后一个元素,该字符串总是在"::"的最后一次出现之后?

使用std::string::rfind()定位::的最后一次出现,并使用std::string::substr()提取令牌:

// Example without confirming that a '::' exists.
std::string last_element(str.substr(str.rfind("::") + 2));