Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# string - creating an unescaped backslash

I am using .NET (C#) code to write to a database that interfaces with a Perl application. When a single quote appears in a string, I need to "escape" it. IOW, the name O'Bannon should convert to O\'Bannon for the database UPDATE. However, all efforts at string manipulation (e.g. .Replace) generate an escape character for the backslash and I end up with O\\'Bannon.

I know it is actually generating the second backslash, because I can read the resulting database field's value (i.e. it is not just the IDE debug value for the string).

How can I get just the single backslash in the output string?

R

like image 661
Randy Cornish Avatar asked Jan 18 '23 18:01

Randy Cornish


2 Answers

Well I did

"O'Bannon".Replace("'","\\'")

and result is

"O\'Bannon"

Is this what you want?

like image 180
Tigran Avatar answered Jan 24 '23 23:01

Tigran


You can use "\\", which is the escape char followed by a backslash.

See the list of Escape Sequences here: http://msdn.microsoft.com/en-us/library/h21280bw.aspx

like image 31
Brissles Avatar answered Jan 24 '23 23:01

Brissles