[Solved] Add \ to a string with quotes in c#


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.

solved Add \ to a string with quotes in c#