问题描述
我有一个包含转义字符返回给我一个字符串。
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
正如你可以看到它包含转义字符。我需要它转换为它的实际价值是
As you can see it contains escape characters. I need it to be converted to its real value which is
任何想法如何做到这一点?
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
.
这篇关于如何将包含字符串转义字符转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!