4

I am trying to put a list of delimiters in a web.config file, one of the delimiters is a carriage return - \r\n. The web.config entry looks like this -

<add key="Separators" value=" |,|;|\r\n"/>

I am trying to read in the list using a call like the following -

string[] MySeparators = ConfigurationManager.AppSettings ConfigSettings.PartNumberSeparators].Split('|');

The list is being read, but the carriage return ends up containing extra back slashes and looks like this - \\r\\n

Is there some way to prevent this from happening?

A Bogus
  • 3,852
  • 11
  • 39
  • 58
  • Note that `\r` in Web.Config is just a string with 2 characters `\` and `r` - there is no escaping unlike in C#.... "Why slashes shown as double slashes when I look at string in debug view" is mandatory daily question on SO. See http://stackoverflow.com/questions/15813454/replace-method-c-sharp-not-working-as-expected/15813489#15813489 – Alexei Levenkov Apr 10 '14 at 20:55

3 Answers3

5

You could try using XML entities &#10; and &#13;. Provided that the .NET configuration system supports resolving these entities, then these should appear as \r\n at runtime.

Martin Costello
  • 9,672
  • 5
  • 60
  • 72
3

First, you forgot the | between \n and \r

Try writing a var like

>var test = "\";

this will give you an error, options are:

>var test = "\\";

>var test = @"\";

Try the second one

  • Hey Thiag0coelho, "\r\n" together represent a carriage return. I did not forget to put a "|" between them. – A Bogus Apr 11 '14 at 12:04
0

You can use System.Text.RegularExpressions.Regex.Unescape, this way:

string[] MySeparators = Regex.Unescape(ConfigurationManager.AppSettings.ConfigSettings.PartNumberSeparators]).Split('|');
Tamim Al Manaseer
  • 3,554
  • 3
  • 24
  • 33