VB.NET中怎么实现汉字转换
本篇文章给大家分享的是有关VB.NET中怎么实现汉字转换,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
下面这段VB.NET汉字转换代码解决了这个问题,将其拷到宏编辑器里保存即可,写的有点傻傻的,懒得优化了,好使就行:)
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports System.Text.
RegularExpressionsPublic Module Unicode2
Character'必须手工选择到单字
Public Sub 单字转换()
Dim doc As Document =
DTE.ActiveDocumentDim docText As TextDocument
= doc.ObjectDim 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
texttext = text.Replace(strValue, "")
strValuestrValue = strValue.
Replace("&", "")
strValuestrValue = strValue.
Replace(" ", "")
strValuestrValue = strValue.
Replace("ChrW(", "")
strValuestrValue = strValue.
Replace(")", "")
Dim chars As Integer =
strValue.Length()
charschars = chars \ 5
Dim strNew As String = ""
For i As Integer = 0 To chars - 1
Dim x As String = strValue.
Substring(i * 5, 5)
strNewstrNew = 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
texttext = text.Replace(strValue, "")
strValuestrValue = strValue.Replace
("&", "")
strValuestrValue = strValue.Replace
(" ", "")
strValuestrValue = strValue.Replace
("ChrW(", "")
strValuestrValue = strValue.Replace
(")", "")
strValuestrValue = 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
texttext = text.Replace(strValue, "")
strValuestrValue = strValue.Replace
("&", "")
strValuestrValue = strValue.Replace
(" ", "")
strValuestrValue = strValue.Replace
("ChrW(", "")
strValuestrValue = strValue.Replace
(")", "")
strValuestrValue = 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.Regular
Expressions.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
texttext = text.Replace(strValue, "")
strValuestrValue = strValue.
Replace("&", "")
strValuestrValue = strValue.
Replace(" ", "")
strValuestrValue = strValue.
Replace("ChrW(", "")
strValuestrValue = strValue.
Replace(")", "")
strValuestrValue = 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
texttext = text.Replace(strValue, "")
strValuestrValue = strValue.
Replace(" ", "")
strValuestrValue = strValue.
Replace("ChrW(", "")
strValuestrValue = 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
以上就是VB.NET中怎么实现汉字转换,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网行业资讯频道。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341