简体   繁体   English

如何将包含转义字符的字符串转换为字符串

[英]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" “测试\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" “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 .该代码依赖于正则表达式来查找所有十六进制数字序列,将它们转换为int ,并将结果值转换为char

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

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

If you want a more general approach ...如果您想要更通用的方法...

HTML Decode HTML 解码

The sample string provided ( "test\40gmail.com" ) is JID escaped .提供的示例字符串( "test\40gmail.com" )是JID 转义的。 It is not malformed, and HttpUtility / WebUtility will not correctly handle this escaping scheme.它不是格式错误的, HttpUtility / WebUtility将无法正确处理此转义方案。

You can certainly do it with string or regex functions, as suggested in the answers from dasblinkenlight and C.Barlow.正如 dasblinkenlight 和 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.我不知道有任何用于解码 JID 转义的 .NET 库,而且简短的搜索也没有出现太多。 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 .我刚刚写了这段代码,它似乎工作得很好......它要求转义序列是十六进制的,并且对值的0x000xFF有效。

// 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. .NET 提供了静态方法 Regex.Unescape 和 Regex.Escape 来执行此任务并再次返回。 Regex.Unescape will do what you need. Regex.Unescape 会做你需要的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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