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

2023-09-04 00:21:22 作者:亡鱼是深海的疤i

我有一个包含转义字符返回给我一个字符串。

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

我似乎有一个小问题,任何人都可以帮忙吗?

I seem to have a small issue, can anyone help?

下面是一个简单的字符串

Here is a sample string

测试\ 40gmail.com

"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

"test@gmail.com"

任何想法如何做到这一点?

Any ideas how to do this?

任何帮助或信息是pciated感激AP $ P $

Any help or information would be gratefully appreciated

推荐答案

如果您正在寻找替换所有转义字符codeS,不仅是$ C $下 @ ,你可以使用这个片段的code进行转换:

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

在code依赖于一个普通的前pression找到十六进制数字的所有序列,将它们转换为 INT ,以及铸造结果值到字符

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.