samedi 18 avril 2020

How to get a substring from a found string to a character in C++?

For example I have a string:

int random_int = 123; // let's pretend this integer could have various values!!

std::string str = "part1 Hello : part2 " + std::to_string(random_int) + " : part3 World ";

All parts are divided by the characters :

Let's say I want to find a substring from "part2" to the next character :, which would return part2 123 in this case.

I know how to find the pos of "part2" by str.find("part2"), but I don't know how to determine the length to the next : from that "part2", because the length can be of various length.

For example, I know that part3 substring could be extracted with str.substr(str.find("part3"));, but only because it's at the end...

So, is there a subtle way to get the substring part2 123 from that string?

Aucun commentaire:

Enregistrer un commentaire