0

I want to replace the string which has \ character in it but is giving me error Newline in constant

How can I do this , Here is my code

string mystring = "Lines xmlns:a=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\";
string tobeReplaced = "Lines xmlns:a=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\";
var newString = mystring.Replace(tobeReplaced, "Lines");
  • 1
    Right, backslashes need escaping - use \\ at the end instead of just \. – Jon Skeet Apr 22 '16 at 10:37
  • Or place "@" before the string - it means 'plain string' – VitaliyK Apr 22 '16 at 10:40
  • Remember that when you double up the backslash, the escaping of the quote character no longer works, this means that this: `"...\"..."` needs to still have a backslash that escapes the quote, but if you *also* want the backslash to be part of the string, you need to do two things, double up the backslash *and* add a backslash to escape the quote, leading to this: `"...\\\"..."` – Lasse V. Karlsen Apr 22 '16 at 10:40
  • A better solution would be to use a verbatim string, `@"Lines xmlns:a=\""http:.....\", but there's lots of things you may have questions about here, best is to read the documentation about string syntax in C# - https://msdn.microsoft.com/en-us/library/362314fe.aspx?f=255&MSPPError=-2147217396 – Lasse V. Karlsen Apr 22 '16 at 10:42

0 Answers0