在unicode char之前撤消转义序列“\”

时间:2014-02-04 12:38:35

标签: c# string unicode char wchar

我使用控制台程序(cmd调用)将标准输入中的字符串转换为从标准输出接收的特殊Unicode字符的字符串。 C#中的返回字符串将转义Unicode字符前的转义反斜杠。

如何撤消此转义?

返回string =

的示例
stdout = "\\x284b\\x2817\\x2801\\x281d\\x2835 \\x281a\\x2801\\x281b\\x281e \\x280a\\x280d \\x2805\\x2815\\x280d\\x280f\\x2807\\x2811\\x281e\\x281e \\x2827\\x2811\\x2817\\x283a\\x2801\\x2813\\x2817\\x2807\\x2815\\x280e\\x281e\\x2811\\x281d \\x285e\\x2801\\x282d"

......但它应该是

stdout = "\x284b\x2817\x2801\x281d\x2835 \x281a\x2801\x281b\x281e \x280a\x280d \x2805\x2815\x280d\x280f\x2807\x2811\x281e\x281e \x2827\x2811\x2817\x283a\x2801\x2813\x2817\x2807\x2815\x280e\x281e\x2811\x281d \x285e\x2801\x282d"

我试图通过

来解决这个问题
var stdout2 = stdout.Replace(@"\\", @"\");

没有效果。

感谢4位帮助。

4 个答案:

答案 0 :(得分:0)

你需要做

stdout = stdout.Replace(@"\\", @"\");

代替。

答案 1 :(得分:0)

我假设您不想删除字符串中的\\。它应打印为\\x284b...。如果是这种情况,请附加@字符串。以下代码将使用\\

打印
       string stdout = @"\\x284b\\x2817\\x2801\\x281d\\x2835 \\x281a\\x2801\\x281b\\x281e
       \\x280a\\x280d \\x2805\\x2815\\x280d\\x280f\\x2807\\x2811\\x281e\\x281e   
       \\x2827\\x2811\\x2817\\x283a\\x2801\\x2813\\x2817\\x2807\\x2815\\x280e\\x281e\\x2811
        \\x281d \\x285e\\x2801\\x282d";

        Console.Write(stdout);
        Console.Read();

答案 2 :(得分:0)

  

结果来自一个名为liblouis的控制台程序

好的,LibLouis有自己奇怪的非标准字符串转义方案,记录在第3节here中。如果要将其转换为原始的非转义Unicode字符串,除了\x之外,还有许多反斜杠转义序列。像(未经测试)的东西:

var escape = new Regex(@"\\(x[0-9A-Fa-f]{4}|y[0-9A-Fa-f]{5}|z[0-9A-Fa-f]{8}|.)");
var chars = new Dictionary<char, string> {
    { 'f', "\f" }, { 'n', "\n" }, { 'r', "\r" }, { 't', "\t" }, { 'v', "\v" },
    { 's', " " }, { 'e', "\x1B"}
};

var decoded_string = escape.Replace(encoded_string, match =>
    match.Length>2 ?
        Char.ConvertFromUtf32(
            int.Parse(
                match.Value.Substring(2),
                System.Globalization.NumberStyles.HexNumber
            )
        ) :
    chars.ContainsKey(match.Value[1]) ?
        chars[match.Value[1]] :
    match.Value.Substring(1)
);

答案 3 :(得分:0)

最后它很容易,同时也有点复杂。我知道可以使用char创建integer来解决问题。因此,通过了解,风格的编码&#39; \ x284b &#39;表示十六进制值&#39; 284B &#39;这是&#39; 10315 &#39;十进制,因此可以转换为char。所以我使用这些小函数将编码转换为Int32,然后将其转换为内部string ...瞧

/// <summary>
/// Gets the char from unicode hexadecimal string.
/// </summary>
/// <param name="characterCode">The character code e.g. '\x2800'.</param>
/// <returns>the current available unicode character if available e.g. ' '</returns>
public static string GetCharFromUnicodeHex(String characterCode)
{

    if (!String.IsNullOrEmpty(characterCode))
    {
        if (characterCode.StartsWith(@"\"))
        {
            characterCode = characterCode.Substring(1);
        }
        if (characterCode.StartsWith("x"))
        {
            characterCode = characterCode.Substring(1);
        }

        int number;
        bool success = Int32.TryParse(characterCode, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out number);

        if (success)
        {
            return GetCharFromUnicodeInt(number);
        }
    }
    return String.Empty;
}


/// <summary>
/// try to parse a char from unicode int.
/// </summary>
/// <param name="number">The number code e.g. 10241.</param>
/// <returns>the char of the given value e.g. ' '</returns>
public static string GetCharFromUnicodeInt(int number)
{
    try
    {
        char c2 = (char)number;
        return c2.ToString();
    }
    catch { }
    return String.Empty;
}