解码Unicode编码字符,如将 \u82f9\u679c 解码为 苹果
Imports System.Text.RegularExpressions

Public Class Form1

    Private Shared Function deChar(ByVal m As Match) As String
        Dim s As String = m.Groups(1).Value
        Dim i As Integer = Convert.ToInt32(s, 16)
        Return ChrW(i).ToString
    End Function
    Shared Function deUniText(ByVal ucode As String) As String
        Return Regex.Replace(ucode, "\\u([a-fA-F0-9]{4})", AddressOf deChar)
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s As String = "This is \u82f9\u679c."
        MsgBox(deUniText(s))
    End Sub

End Class

妈的SB了,原来正则类中的 Regex.Unescape 可以直接解码Unicode字符。
Dim s As String = Regex.Unescape("This is \u82f9\u679c.")
MsgBox(s)
编程的本质就是重复造轮子!
不过此方法没有错误预处理,如果字符不符合预期有可能会报错。


另外再写一个解码\Uxxxxxxxx(32位Unicode字符)的函数吧,Regex.Unescape默认不支持该编码,正好可以弥补一下!
Dim ret = Regex.Replace("This is \U000082f9\U0000679c.", "\\U([0-9a-fA-F]{8})",
    Function(m)
        Dim i = Integer.Parse(m.Groups(1).Value, Globalization.NumberStyles.HexNumber)
        'Dim i = CInt("&H" & m.Groups(1).Value)
        Return Char.ConvertFromUtf32(i)
    End Function
)