To erase the part of a string, you have to find where is that part begins and ends. Finding somethig inside an std::string
is very easy because the class have six buit-in methods for this (std::string::find_first_of
, std::string::find_last_of
, etc.). Here is a small example of how your problem can be solved:
#include <iostream>
#include <string>
int main() {
std::string input { " \\PATH\\MYFILES This is my sting " };
auto pos = input.find_last_of('\\');
if(pos != std::string::npos) {
input.erase(0, pos + 1);
pos = input.find_first_of(' ');
if(pos != std::string::npos)
input.erase(pos);
}
std::cout << input << std::endl;
}
Note: watch out for escape sequences, a single backslash is written as "\\"
inside a string literal.
solved Getting a word or sub string from main string when char ‘\’ from RHS is found and then erase rest