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

"[email protected]"

How can I do this?

3
  • 3
    Is this the only case, or are there many different control characters that you'll have to account for? Commented Jul 20, 2012 at 17:27
  • Both HttpUtility and WebUtility have decoding methods - please reference this stackoverflow.com/questions/122641/… Commented Jul 20, 2012 at 17:30
  • 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? Commented Jul 20, 2012 at 17:31

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm a little confused with regex and C# (so many escape characters). Could you possibly post the regex string by itself (not formatted for C# use)?
@James Sure, it's \\([0-9A-Fa-f]+) (here, the slash is escaped for the regex engine, but not for the compiler).
It's handy when working with regexes to use a C# "verbatim string literal".. e.g. var rx = new Regex(@"\\([0-9A-Fa-f]+)");
res.Append(src.Substring(pos, m.Index)); should be res.Append(src.Substring(pos, m.Index - pos));
2

.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

Comments

0
string test = "test\40gmail.com";

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

If you want a more general approach ...

HTML Decode

5 Comments

please explain how it would not
he probably saw your previous answer prior to the edit.. your answer will work Micah
When I first saw it, it had test.replace("\40","");, and @dasblinkenlight's commment would be true.
You need @"\40" (same with test's declaration) so that C# takes it verbatim.
I downvoted this because the answer applies only to the sample string, and not to the general case.
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.

3 Comments

You are assuming that he is using Jabber / chat client when this is not stated
@MicahArmantrout No, I'm not. I didn't say he was using that client, I said he has strings escaped in the same fashion. Using \(hex value) to indicate an escaped character is not platform dependent.
@MicahArmantrout No. I am absolutely not making that assumption.
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.