Using Regex.Escape

Hi,

I got these two lines of code:

        1. text = text.Replace("\", Regex.Escape("\"));
        2. text = text.Replace("'", "\'");

The first line give me a compiling error:
         error CS1010: Newline in constant

Rem out the first line.  Run w/ the debugger the second line did not work, it didnot replace ' with \'

Please advise.

Diane Ha

tnova1Asked:
Who is Participating?
 
hongjunCommented:
2. should be

text = text.Replace("'", "\\'");
0
 
hongjunCommented:
1. should be

text = Regex.Replace(text, Regex.Escape("\\"));
0
 
tnova1Author Commented:
Hi hongjun

    The correct answer for the first line:
          text.Replace("\\", Regex.Escape("\\"));

     Your 2nd line is right.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.