阳光网驿-企业信息化交流平台【DTC零售连锁全渠道解决方案】

 找回密码
 注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

手机号码,快捷登录

老司机
查看: 1936|回复: 0

[推荐] VB.NET中ChrW函数转换到汉字

[复制链接]
  • TA的每日心情
    开心
    2021-8-30 00:00
  • 签到天数: 35 天

    [LV.5]常住居民I

    发表于 2009-10-16 14:56:46 | 显示全部楼层 |阅读模式
    '若使用Reflector,而且程序中有中文字符,Reflector会用代码表示。若反成C#,网上有很多解决方案了,而VB.net却找不到.
    'VB.net里可能更复杂些,毕竟C#还是一个完整的字符串,而VB中却被拆成一个一个单字,然后用&连接
    '下面这段代码解决了这个问题,将其拷到宏编辑器里保存即可
    '写的有点傻傻的,懒得优化了,好使就行

    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports System.Diagnostics
    Imports System.Text.RegularExpressions
    Public Module Unicode2Character
        '必须手工选择到单字
        Public Sub 单字转换()
            Dim doc As Document = DTE.ActiveDocument
            Dim docText As TextDocument = doc.Object
            Dim selText As TextSelection = docText.Selection()
            Dim text As String = selText.Text
            Dim ch As Char = cc(text)
            docText.ReplacePattern(text, ch)
        End Sub
        '全部替换当前文件的汉字
        Public Sub 全部转换()
            Dim doc As Document = DTE.ActiveDocument
            Dim docText As TextDocument = doc.Object
            Dim selText As TextSelection = docText.Selection()
            selText.SelectAll()
            Dim text As String = selText.Text
            Dim iLength As Integer
            Do
                iLength = text.Length
                Dim m As Text.RegularExpressions.Match
                '先找“字符串”:ChrW(12345) & ChrW(23456) ... & ChrW(56789)
                Dim strPattern As String = "(ChrW\([0-9]{5}\)\s&\s)+ChrW\([0-9]{5}\)"
                m = Regex.Match(text, strPattern, RegexOptions.IgnoreCase)
                If m.Success Then
                    Dim strMatch As String = m.Value
                    Dim strValue As String = m.Value
                    text = text.Replace(strValue, "")
                    strValue = strValue.Replace("&", "")
                    strValue = strValue.Replace(" ", "")
                    strValue = strValue.Replace("ChrW(", "")
                    strValue = strValue.Replace(")", "")
                    Dim chars As Integer = strValue.Length()
                    chars = chars \ 5
                    Dim strNew As String = ""
                    For i As Integer = 0 To chars - 1
                        Dim x As String = strValue.Substring(i * 5, 5)
                        strNew = strNew & ic(x)
                    Next
                    docText.ReplacePattern(strMatch, """" & strNew & """")
                Else
                    Exit Do
                End If
                If Not text.Length < iLength Then
                    Exit Do
                End If
            Loop

            selText.SelectAll()
            text = selText.Text
            Do
                iLength = text.Length
                Dim m As Text.RegularExpressions.Match
                '再找单字:" & ChrW(23456) & "
                Dim strPattern As String = "\""\s&\sChrW\([0-9]{5}\)\s&\s\"""
                m = Regex.Match(text, strPattern, RegexOptions.IgnoreCase)
                If m.Success Then
                    Dim strMatch As String = m.Value
                    Dim strValue As String = m.Value
                    text = text.Replace(strValue, "")
                    strValue = strValue.Replace("&", "")
                    strValue = strValue.Replace(" ", "")
                    strValue = strValue.Replace("ChrW(", "")
                    strValue = strValue.Replace(")", "")
                    strValue = strValue.Replace("""", "")
                    Dim strNew = ic(strValue)
                    docText.ReplacePattern(strMatch, strNew)
                Else
                    Exit Do
                End If
                If Not text.Length < iLength Then
                    Exit Do
                End If
            Loop

            selText.SelectAll()
            text = selText.Text
            Do
                iLength = text.Length
                Dim m As Text.RegularExpressions.Match
                '再找单字:" & ChrW(23456)
                Dim strPattern As String = "\""\s&\sChrW\([0-9]{5}\)"
                m = Regex.Match(text, strPattern, RegexOptions.IgnoreCase)
                If m.Success Then
                    Dim strMatch As String = m.Value
                    Dim strValue As String = m.Value
                    text = text.Replace(strValue, "")
                    strValue = strValue.Replace("&", "")
                    strValue = strValue.Replace(" ", "")
                    strValue = strValue.Replace("ChrW(", "")
                    strValue = strValue.Replace(")", "")
                    strValue = strValue.Replace("""", "")
                    Dim strNew = ic(strValue)
                    docText.ReplacePattern(strMatch, strNew & """")
                Else
                    Exit Do
                End If
                If Not text.Length < iLength Then
                    Exit Do
                End If
            Loop

            selText.SelectAll()
            text = selText.Text
            Do
                iLength = text.Length
                Dim m As Text.RegularExpressions.Match
                '再找单字:ChrW(23456) & "
                Dim strPattern As String = "ChrW\([0-9]{5}\)\s&\s\"""
                m = Regex.Match(text, strPattern, RegexOptions.IgnoreCase)
                If m.Success Then
                    Dim strMatch As String = m.Value
                    Dim strValue As String = m.Value
                    text = text.Replace(strValue, "")
                    strValue = strValue.Replace("&", "")
                    strValue = strValue.Replace(" ", "")
                    strValue = strValue.Replace("ChrW(", "")
                    strValue = strValue.Replace(")", "")
                    strValue = strValue.Replace("""", "")
                    Dim strNew = ic(strValue)
                    docText.ReplacePattern(strMatch, """" & strNew)
                Else
                    Exit Do
                End If
                If Not text.Length < iLength Then
                    Exit Do
                End If
            Loop
            selText.SelectAll()
            text = selText.Text
            Do
                iLength = text.Length
                Dim m As Text.RegularExpressions.Match
                '最后单字:ChrW(23456)
                Dim strPattern As String = "ChrW\([0-9]{5}\)"
                m = Regex.Match(text, strPattern, RegexOptions.IgnoreCase)
                If m.Success Then
                    Dim strMatch As String = m.Value
                    Dim strValue As String = m.Value
                    text = text.Replace(strValue, "")
                    strValue = strValue.Replace(" ", "")
                    strValue = strValue.Replace("ChrW(", "")
                    strValue = strValue.Replace(")", "")
                    Dim strNew = ic(strValue)
                    docText.ReplacePattern(strMatch, """" & strNew & """")
                Else
                    Exit Do
                End If
                If Not text.Length < iLength Then
                    Exit Do
                End If
            Loop
        End Sub
        Private Function cc(ByVal str As String) As Char
            Dim int As Integer = CInt(str.Substring(5, 5))
            Dim ch As Char = ChrW(int)
            Return ch
        End Function
        Private Function ic(ByVal int As Integer) As Char
            Dim ch As Char = ChrW(int)
            Return ch
        End Function

    End Module

    附:
    可以进行charcode值与字符之间互相转换
    http://www.geocities.com/george_ruban/charset/CharCode.html

    希望能对大家有些帮助
    楼主热帖
    启用邀请码注册,提高发帖质量,建设交流社区
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表