C++

Asked • 08/23/19

What's the best way to trim std::string?

I'm currently using the following code to right-trim all the `std::strings` in my programs: std::string s; s.erase(s.find_last_not_of(" \\n\\r\ ")+1); It works fine, but I wonder if there are some end-cases where it might fail? Of course, answers with elegant alternatives and also left-trim solution are welcome.

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.