in a C++ program, there is a point when it reads a string like:
"NONAME_1_1\r"
the \r
is causing me trouble. I guess it prints or adds something like "^M". Is it right? Anyway it casues me problem, and I want to get rid of it.
I can not modify the input. I wonder how could I at this point, using C++, and in the easiest way, to remove \r
for this string.
I know how to do it on bash but no clue on C++.
Thanks.
Try this. text = text . Replace("\\r\\n", "");
You can either use R base function gsub() or use str_replace() from stringr package to remove characters from a string or text.
I'm assuming that by string, you mean std::string
.
If it's only the last character of the string that needs removing you can do:
mystring.pop_back();
mystring.erase(mystring.size() - 1);
Edit: pop_back()
is the next version of C++, sorry.
With some checking:
if (!mystring.empty() && mystring[mystring.size() - 1] == '\r')
mystring.erase(mystring.size() - 1);
If you want to remove all \r
, you can use:
mystring.erase( std::remove(mystring.begin(), mystring.end(), '\r'), mystring.end() );
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