-6

I need to add a '\' to a string

I try this

 var Filename = name.Replace("'", "\'");
 name = Filename ;

if name = he's here

name.Replace("'", "\'") will return : "he\\'s here"

what I need is: he\'s here

melom
  • 107
  • 1
  • 13

1 Answers1

0

First name.Replace("'", "\'") does nothing because "'" == "\'". So name.Replace("'", "\'") returns "he's here" (you can try it in https://dotnetfiddle.net/). What you want is: name.Replace("'", "\\'")

Second if you inspect name in the debugger (in watch window or immediate window) you will get "he\\'s here" because that is how you should write a string constant in c# to get he\'s here into a variable.

P. Kouvarakis
  • 1,893
  • 12
  • 21