0

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.

  • `Note: The string Values I'm getting are pulled from Visual Studio's quickwatch feature, where I am inspecting Line and copying the value.` QuickWatch doesn't show the string 'as is'. Click the magnifying glass at the end of the row and then `Text Visualizer` to see the **actual** value. – mjwills Sep 19 '18 at 21:08
  • I stand corrected. The only thing we can say is that `Line` does not contain that string: https://dotnetfiddle.net/zIsS18 – Camilo Terevinto Sep 19 '18 at 21:10
  • You want to use `Line = Line.Replace(@"\(Y/A M D/J\", "");` – mjwills Sep 19 '18 at 21:11
  • 1
    You are using the verbatim string, and still escaping the `\\` character at the same time... the quickwatch assumes you are not using verbatim. – NetMage Sep 19 '18 at 21:12
  • @NetMage, that's the answer. – Poul Bak Sep 19 '18 at 21:14
  • mjwills and NetMage had the correct answer, VS was adding the additional backslashes to escape the existing ones in their text previewer. – Stephen DeBoer Sep 19 '18 at 21:15

1 Answers1

0

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

Jesse Johnson
  • 1,638
  • 15
  • 25
  • Thank you for the input, The comments in the question also lead me to the correct answer, where VS was adding extra escape characters to the string. – Stephen DeBoer Sep 19 '18 at 21:18