-
VB.NET中ChrW函数转换到汉字
'若使用Reflector,而且程序中有中文字符,Reflector会用代码表示。若反成C#,网上有很多解决方案了,而VB.net却找不到.
'VB.net里可能更复杂些,毕竟C#还是一个完整的字符串,而VB中却被拆成一个一个单字,然后用&连接
'下面这段代码解决了这个问题,将其拷到宏编辑器里保存即可
'写的有点傻傻的,懒得优化了,好使就行:)
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports System.Text.RegularExpressions
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()
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
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 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
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
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
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
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
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
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
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
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
出处:https://www.cnblogs.com/zgqys1980/archive/2006/12/08/586125.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式