I'm working on C++,
i have one string as follows:
string str = "rake::may.chipola::ninbn::myFuntion";
How to get last element from above string which is always after the last occurrence of "::"?
You can use string. back() to get a reference to the last character in the string. The last character of the string is the first character in the reversed string, so string. rbegin() will give you an iterator to the last character.
The common solution to tokenize a string in C++ is using std::istringstream , which is a stream class to operate on strings. The following code extract tokens from the stream using the extraction operator and insert them into a container.
token = strtok(Input, "-"); strcpy(first, token); token = strtok(NULL, "-"); token = strtok(Input, "."); strcpy(name, token); token = strtok(NULL, ".");
Use std::string::rfind()
to locate the last occurrence of ::
and use std::string::substr()
to extract the token:
// Example without confirming that a '::' exists.
std::string last_element(str.substr(str.rfind("::") + 2));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With