How to merge or insert the "@" character in a string, including escape characters, without defining a varibale string from scratch in C #

Hi, I have two related questions.

1) suppose that:

string strMessage = "\ nHellow \ n \ nWorld"; console.writeln (strMessage);
Result:


Hello


World


Now, if we want to show the string in its original format in One Line, we must redefine the first variable from scratch.


string strOrignelMessage = @ "\ nHellow \ n \ nWorld";

console.writln (strOrignelMessage);


Result: \ nHellow \ n \ nWorld ---------------------> and everything is in order.

I am wondering if there is a way to avoid defining a new variable (strOrignelMessage) in the code for this purpose and only using only the first string variable (strMessage) and apply some tricks and print them on one line.

I tried the following workaround first, but it causes some errors. Suppose we have:


string strMessage = "a \ aa \ nbb \ nc \ rccc";

string strOrigenalMessage = strMessage.replace ("\ n", "\\ n"). replace ("\ r", "\\ r");

Console.writeln (strOrigenalMessage)


the result is: aa \ nbb \ nc \ rccc

note that before the first "\" is not printed . Now my second question:

2) How can we fix a new problem with a single "\" in the line

I hope this problem is resolved correctly, and my explanations will be enough, thanks

+4
source share
4 answers

No, because the compiler has already converted all of your escaped characters in the source string to the characters that they represent. After that, itโ€™s too late to convert them to non-special characters. You can search and replace by converting '\ n' to literally @ "\ n", but it will hit hard and you better define the line correctly first. If you want to avoid backslashes in the first place, why not put each backslash character in front of you:

Instead of "\ n" use "\\ n".

Updated in response to your comment:

If the line comes from user input, you do not need to hide the backslash, because it will be saved as a backslash in the input line. The control character only works as an escape character in string literals in code (and does not precede @, which makes them literal string literals).

+4
source

if you want "\ n \ n \ a \ a \ r \ blah" to print as \n\n\a\a\r\blah without @ , just replace everything \ with \\

\ is an escaper in a line other than verbal. Therefore, you just need to avoid the escoper as soon as possible.

+1
source

If you want to use both lines, but want to have only one code, write a line with @ and create another using Replace (@ "\ n", "\ n").

0
source

explanations for Anthony Pegram (if I understand correctly) and everyone who found this useful

I think I found my question in question2.

at first, unfortunately, I thought that escape characters are limited to \ n, \ t, \ r, \ v and this made me confuseded becouse in my sample line, which I used \ a and \ b and the compiler behavior was not clear for me.

but finally, I found that \ a and \ b are in escape characters set too. If you use "\" without escap characters, a compile-time error would be raised (this is so funny when I think about my error again)

Pls refer to this useful msdn article for more information.

2.4.4.5 String literals

and you could not replace \ (single \) with \\ Because basically you could not have (single \) without using escape characters after it in the .so line, we cannot write such a line in the code:

string strTest = "abc \ pwww"; ------> compile time error

and to extract inactivated escape characters the version of the string, we can simply use the string.replace method, as I used befor.

excuse me for a long time, thank you all for your cooperation.

0
source

All Articles