I'm having a small brain fart: I'd like to remove all instances of the newline character '\n'
in a std::string
. I'd prefer to use the STL instead of manual, multi-nested for loops; the only problem is I've forgotten how...
Would for(...) { std::string::remove_if(...); } ;
work? Could I need to use std::for_each(...,..., std::string::remove_if(...));
? Or would something else be required?
First idea: the remove/erase idiom:
str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
If you have Boost.Range it works even shorter:
#include <boost\range\algorithm_ext\erase.hpp>
boost::remove_erase(str, '\n');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With