0

I have a program that reads from a xml document. In this xml document some of the attributes contain special characters like "\n", "\t", etc.

Is there an easy way to replace all of these strings with the actual character or do I just have to do it manually for each character like the following example?

Manual example:

s.Replace("\\n", "\n").Replace("\\t", "\t")...

edit:

I'm looking for some way to treat the string like an escaped string like this(even though I know this doesn't work)

s.Replace("\\", "\");
Tony Brix
  • 4,085
  • 7
  • 41
  • 53

2 Answers2

2

Try Regex.Unescape().

Official docs here: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.unescape(v=vs.110).aspx

Manuel Spezzani
  • 1,041
  • 7
  • 15
0

Why not just walk the document and build up the new string in one pass. Saves a lot of duplicate searching and intermediate allocations

string ConvertSpecialCharacters(string input) {
  var builder = new StringBuilder();
  bool inEscape = false;
  for (int i = 0; i < input.Length ; i++) {
    if (inEscape) {
     switch (input[i]) {
      case 'n':  
        builder.Append('\t');
        break;
      case 't':
        builder.Append('\n');
        break;
      default:
        builder.Append('\\');
        builder.Append(input[i]);
    }
    else if (input[i] == '\\' && i + 1 < input.Length) {
      inEscape = true;
    }
    else {
      builder.Append(input[i]);
    } 
  }
  return builder.ToString();
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • but that would require me to enter all escapable characters as cases. I am looking for a way to not do that. I edited the question to show what I am looking for. – Tony Brix Mar 21 '14 at 00:35