Reputation: 23
I was trying to do what i wrote in the title only that I have a problem.. when the program goes to read a string for remove the strings that have space in front of him, removes all those who have a space until he get to the string that does not have a space.. i tried this:
string s = "hello my name is SOMETHING";
size_t space_pos = s.rfind(" ") + 1;
cout << s.substr(space_pos) << "\n";
Output:
SOMETHING
I need the result to be: "name is SOMETHING". I tried with replace, but the lenght of first 2 strings ("hello" and "my") it always varies like, from "hello" to "hello1" or "hello2818+".
Thanks you and sorry for my bad english.
Upvotes: 1
Views: 44
Reputation: 76
rfind() find the last occurrence of the string given. In that program you are creating a substring with only the value between the last occurrence and the end of the string.
string s = "hello my name is SOMETHING";
size_t space_pos = s.find(" ") + 1;
for(int i=0;i<2;i++){
s=s.substr(space_pos+1);
space_pos = s.find(" ");
}
std::cout<<s;
Doing that you erase the first three spaces.
Upvotes: 1