If I have a string and I want to delete the last token after the char '/'. I managed to get the tokens one by one. But how do I format the string? Code is given below for finding tokens:
std::string s = "/dir1/dir2/dir3";
std::string delimiter = "/";
size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
std::cout << token << std::endl;
s.erase(0, pos + delimiter.length());
}
std::cout << s << std::endl
I want my formated string and output
to be "/dir1/dir2"
so, I delete the last token and '/' so, "/dir3"
I have one more condition:
when it is the last token the '/' should not be removed. So, when I want to remove dir1, the value of s
to only be '/' so, not empty.