How to convert a string containing escape characters to a string

前端 未结 4 2209
心在旅途
心在旅途 2020-12-11 19:25

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

Here is a sample string

\"test\\40gmail.com\"

A

相关标签:
4条回答
  • 2020-12-11 19:37
    string test = "test\40gmail.com";
    
    test.replace(@"\40","@");
    

    If you want a more general approach ...

    HTML Decode

    0 讨论(0)
  • 2020-12-11 19:39

    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.

    0 讨论(0)
  • 2020-12-11 19:47

    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.

    0 讨论(0)
  • 2020-12-11 19:52

    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;
    }
    
    0 讨论(0)
提交回复
热议问题