简体   繁体   中英

How to convert a string containing escape characters to a string

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?

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 .

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

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

If you want a more general approach ...

HTML Decode

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.

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;
}

.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://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.unescape

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM