I am attempting to do some string manipulations and I have a need to take multi-line strings and create a List<String> by splitting on \r\n so that I end up with a list of the lines found in the string.
However, after doing the Regex.Split I’m ending up with lines that have every single
" escaped with a \.
For example, a snippet of an input string is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<g2s:g2sMessage xmlns:g2s="http://www.gamingstandards.com/g2s/schemas/v1.0.3">
<g2s:g2sBody g2s:dateTimeSent="2012-11-06T09:12:38.006-08:00" g2s:egmId="RBG_1234"
g2s:hostId="1">
<g2s:communications g2s:commandId="37102" g2s:dateTime="2012-11-06T09:12:38.004-08:00"
g2s:deviceId="1"
g2s:errorCode="G2S_none"
When I do the following to split this string into a List<String> using:
List<string> lines = new List<string>(Regex.Split(message, "\r\n"));
The result is:
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
<g2s:g2sMessage xmlns:g2s=\"http://www.gamingstandards.com/g2s/schemas/v1.0.3\">
<g2s:g2sBody g2s:dateTimeSent=\"2012-11-06T09:12:38.006-08:00\" g2s:egmId=\"RBG_1234\"
g2s:hostId=\"1\">
<g2s:communications g2s:commandId=\"37102\" g2s:dateTime=\"2012-11-06T09:12:38.004-08:00\"
g2s:deviceId=\"1\"
g2s:errorCode=\"G2S_none\"
How would I do this without ending up with strings that have " escaped?
Is C# really escaping the characters, or is it just the way the debugger is showing the strings?
This is just the way that the string is displayed inside the debugger (Visual Studio, I guess?). Those backslashes aren’t really in your string. You can prove this to yourself by outputting the string to the console or to a file, and noticing that the backslashes don’t appear.