6

I have a string that is returned to me which contains escape characters.

Here is a sample string

"test\40gmail.com"

As you can see it contains escape characters. I need it to be converted to its real value which is

"test@gmail.com"

How can I do this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Martin
  • 23,844
  • 55
  • 201
  • 327
  • 3
    Is this the only case, or are there many different control characters that you'll have to account for? – Jason Down Jul 20 '12 at 17:27
  • 1
    Are you working in a web environment? (i.e. do you already have a reference to System.Web in your project). Or do you need something that is independent? – James Jul 20 '12 at 17:31
  • Both HttpUtility and WebUtility have decoding methods - please reference this https://stackoverflow.com/questions/122641/how-can-i-decode-html-characters-in-c – Mike Perrenoud Jul 20 '12 at 17:30

5 Answers5

10

If you are looking to replace all escaped character codes, not only the code for @, you can use this snippet of code to do the conversion:

public static string UnescapeCodes(string src) {
    var rx = new Regex("\\\\([0-9A-Fa-f]+)");
    var res = new StringBuilder();
    var pos = 0;
    foreach (Match m in rx.Matches(src)) {
        res.Append(src.Substring(pos, m.Index - pos));
        pos = m.Index + m.Length;
        res.Append((char)Convert.ToInt32(m.Groups[1].ToString(), 16));
    }
    res.Append(src.Substring(pos));
    return res.ToString();
}

The code relies on a regular expression to find all sequences of hex digits, converting them to int, and casting the resultant value to a char.

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

.NET provides the static methods Regex.Unescape and Regex.Escape to perform this task and back again. Regex.Unescape will do what you need.

https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.unescape

Ben Gripka
  • 16,012
  • 6
  • 45
  • 41
0
string test = "test\40gmail.com";

test.replace(@"\40","@");

If you want a more general approach ...

HTML Decode

Micah Armantrout
  • 6,781
  • 4
  • 40
  • 66
0

The sample string provided ("test\40gmail.com") is JID escaped. It is not malformed, and HttpUtility/WebUtility will not correctly handle this escaping scheme.

You can certainly do it with string or regex functions, as suggested in the answers from dasblinkenlight and C.Barlow. This is probably the cleanest way to achieve the desired result. I'm not aware of any .NET libraries for decoding JID escaping, and a brief search hasn't turned up much. Here is a link to some source which may be useful, though.

Esoteric Screen Name
  • 6,082
  • 4
  • 29
  • 38
0

I just wrote this piece of code and it seems to work beautifully... It requires that the escape sequence is in HEX, and is valid for value's 0x00 to 0xFF.

// Example
str = remEscChars(@"Test\x0D") // str = "Test\r"

Here is the code.

private string remEscChars(string str)
{
   int pos = 0;
   string subStr = null;
   string escStr = null;

   try
   {
      while ((pos = str.IndexOf(@"\x")) >= 0)
      {
         subStr = str.Substring(pos + 2, 2);
         escStr = Convert.ToString(Convert.ToChar(Convert.ToInt32(subStr, 16)));
         str = str.Replace(@"\x" + subStr, escStr);
      }
   }
   catch (Exception ex)
   {
      throw ex;
   }

   return str;
}
Willie Visagie
  • 171
  • 1
  • 14