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?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81
  • As @karim pointed out, there is already an answer there, another option if to just use `find_last_not_of` combined with `substr`. – ichramm Jun 01 '14 at 00: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. – Lightness Races in Orbit Jun 01 '14 at 01:33

0 Answers0