How to Replace CR\LF

G

groleo

Hi.

I have the following string:

string s = "hello\W\n";

If I do this: std::cout << s; i would get "helloW"
How can I replace the \ within the string, so when I print it out, I
would get:

"hello\W"??

I tried like this:
string backslash ="\\";
unsigned int loc = 0;
do{
loc = until_regex.find( backslash[0], 0 );
if ( loc == string::npos)
break;
until_regex.replace( loc, 2,"A" );
}while( loc != string::npos );

but no luck.
 
E

Evan

The \ before the W is dropped at compile time. You need to escape it in
the declaration of s:
string s = "hello\\W\n";
 
G

groleo

Well , yes i know that the \ before W is escaped.
But the problem is that s is a user input, representing a
regexp

If i would have a regexp like this:
s = "\w+\W*"

the user should input something like:
s= "\\w+\\W*"
which would become confusing, since the \ in regexp needs to be escaped
too.
 
M

msalters

Well , yes i know that the \ before W is escaped.
But the problem is that s is a user input, representing a
regexp

If i would have a regexp like this:
s = "\w+\W*"

the user should input something like:
s= "\\w+\\W*"
which would become confusing, since the \ in regexp needs to be escaped
too.

No. The need to escape \ exists only at compile-time. I.e. when
it occurs in your .cpp. Users can just enter a single \ and this
will appear as a single char in your std::string. If they use the
TAB key, you'll get the single char '\t' in your std::string. The
'\"' char can also be entered with a single keystroke (on most
keyboards). OTOH, some keyboards need multiple keystrokes to
enter a single accented char like ä (which for me is "+a)

Regards,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top