1

I am trying to remove trailing whitespaces of a string in a concise/efficient way.

Let's say I have the string:

string input= "2 2 + " 

I used this in an attempt to make it input = "2 2+"

 std::string::iterator end_pos = std::remove(input.begin(), input.end(), ' '); input.erase(end_pos, input.end()); cout << input << endl; 

However, it removed all the whitespaces.

How would I implement it so it only removes trailing whitespaces?

2
  • As @karim pointed out, there is already an answer there, another option if to just use find_last_not_of combined with substr. Commented Jun 1, 2014 at 0:39
  • 1
    "Extracting" implies that you want to keep the spaces, whereas in fact the opposite is true. We call this "trimming". If you'd searched for that, you'd have found the answer very quickly. :) So now you know. Commented Jun 1, 2014 at 1:33

0