0

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

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

3 回答 3

0

您忘记转义您手动打印的字符串。因此特殊字符 '\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);
于 2013-07-26T09:46:57.457 回答
0

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

我实际上希望输出是“£ £”而不是“£ \u00a3377m”。

为此,我最终使用了Replace unicode escape sequences in a string的答案,该字符串使用以下函数:

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);
}
于 2013-07-26T10:32:53.920 回答
0

00A3£字符的unicode。看看http://unicode-table.com/en/#00A3

因此,当您尝试编写 " 时\u00a3377m"常规字符串文字将是£377m.

使用verbtaim 字符串文字,而不是喜欢;

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

我完全忘记添加我实际上想要£符号的问题

char c = '\u00a3';
string s = c.ToString(); // s will be £
于 2013-07-26T10:02:55.043 回答