C#UTF-8字符编码输出-变量与字符串不同

C#UTF-8字符编码输出-变量与字符串不同,c#,.net,encoding,utf-8,C#,.net,Encoding,Utf 8,这是一个简单的测试用例,我觉得我缺少了一些基本的东西,但任何帮助都将不胜感激 string data = @"Well done UK building industry, Olympics \u00a3377m under budget + boost"; foreach (Match m in Regex.Matches(data, @"\\u(\w*)\b")) { Console.WriteLine("'{0}' found at index {1}.", m.Value, m.

这是一个简单的测试用例,我觉得我缺少了一些基本的东西,但任何帮助都将不胜感激

string data = @"Well done UK building industry, Olympics \u00a3377m under budget + boost";
foreach (Match m in Regex.Matches(data, @"\\u(\w*)\b"))
{
    Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
    string match = m.Value;
    // These should output the exact same thing however the first is a £ and the other is \u00a3377m
    Console.WriteLine("\u00a3377m" + "      " + match);
}

您忘记转义正在手动打印的字符串。因此,特殊字符“\u00a3377m”被直接解析

以下工作符合要求:

// These should output the exact same thing however the first is a £ and the other is \u00a3377m
            Console.WriteLine("\\u00a3377m" + "      " + match);
另一个选项是使用@:

Console.WriteLine(@"\u00a3377m" + "      " + match);

00A3
字符的unicode。看一看

因此,当您尝试编写“
\u00a3377m”
时,将是
377m

使用verbtaim字符串文字,而不是像

Console.WriteLine(@"\u00a3377m" + "      " + match);
我完全忘了补充一个问题,那就是我实际上想要 ·标志


我很感谢你的帮助,但这是我的错,因为我错过了一些关键信息

实际上,我希望输出为“镑”,而不是“\u00a337m”

为此,我最终使用的答案是使用以下函数:

private static Regex _regex = new Regex(@"\\u(?<Value>[a-zA-Z0-9]{4})", RegexOptions.Compiled);
public string Decoder(string value)
{
    return _regex.Replace(
        value,
        m => ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString()
    );
}

确实很基本。您错过了将
数据
转换为文本字符串的
@
。很抱歉,回顾过去,我真的没有很好地解释我要做的事情,我知道我在把数据变成文字,我只是这样做了,所以我没有\\u00a337m,这可能会让一些人感到困惑。我实际上想做的是让输出匹配一个符号,就像手动输入字符串一样,我最终实现这一点的方法是使用来自的函数。谢谢你的帮助。是的,很抱歉,我完全忘了在问题上加上我想要的是符号,而不是文字。“我将回答这个问题,解释我是如何做到的。”Jordan更新了我的答案。这看起来是一个很好的解决方案,我没有意识到.ToString函数可以这样使用!谢谢你提供的信息。
private static Regex _regex = new Regex(@"\\u(?<Value>[a-zA-Z0-9]{4})", RegexOptions.Compiled);
public string Decoder(string value)
{
    return _regex.Replace(
        value,
        m => ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString()
    );
}
string data = @"Well done UK building industry, Olympics \u00a3377m under budget + boost";
foreach (Match m in Regex.Matches(data, @"\\u(\w*)\b"))
{
    Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
    string match = m.Value;
    //Decode the string so we no longer have \u values
    match = Decoder(match);
    // These should output the exact same thing however the first is a £ and the other is \u00a3377m
    Console.WriteLine("\u00a3377m" + "      " + match);
}