String.replace with verbatim string

418 Views Asked by At

I have a string which has several occurrences of the following text, exactly as it appears:

\\(Y/A M D/J\\

The unclosed parenthesis is causing issues, so I thought I would remove the offending section, as I do not need that portion for my use of the larger string.

I've attempted to use the following line to remove the text, however Line is identical before and after running:

Line = Line.Replace(@"\\(Y/A M D/J\\", "");

I've used the verbatim string Identifier to avoid confusion with having to escape the special characters manually. Is there something special required to do a string.replace() when working with verbatim strings?

Note: The string Values I'm getting are pulled from Visual Studio's quickwatch feature, where I am inspecting Line and copying the value.

1

There are 1 best solutions below

1
On

Are you sure that the \\ in the original string does not include escape characters? The following snippet works as you desire but assumes the original value, input, is exactly as shown. The result is output will contain some more text:

var input = @"\\(Y/A M D/J\\some more text";
var output = input.Replace(@"\\(Y/A M D/J\\", "");

Link to .NET Fiddle to demonstrate