VB.NET汉字转换详细操作步骤介绍

开发 后端
我们在运用能够VB.NET进行实际开发的过程中,会逐渐的发现,这是一个非常实用的开发语言。我们先从VB.NET汉字转换的实现方法来解读其中的应用概念。

VB.NET开发环境的出现,帮助开发人员实现了许多特定的功能需求。比如在对移动设备的操作方面等。VB.NET汉字转换中若使用Reflector,而且程序中有中文字符,Reflector会用代码表示。若反成C#,网上有很多解决方案了,而VB.NET却找不到。VB.NET里可能更复杂些,毕竟C#还是一个完整的字符串,而VB中却被拆成一个一个单字,然后用&连接。#t#

下面这段VB.NET汉字转换代码解决了这个问题,将其拷到宏编辑器里保存即可,写的有点傻傻的,懒得优化了,好使就行:)

  1. Imports System  
  2. Imports EnvDTE  
  3. Imports EnvDTE80  
  4. Imports System.Diagnostics  
  5. Imports System.Text.
    RegularExpressions  
  6. Public Module Unicode2
    Character  
  7. '必须手工选择到单字  
  8. Public Sub 单字转换()  
  9. Dim doc As Document = 
    DTE.ActiveDocument  
  10. Dim docText As TextDocument 
    doc.Object  
  11. Dim selText As TextSelection 
    docText.Selection()  
  12. Dim text As String = selText.Text  
  13. Dim ch As Char = cc(text)  
  14. docText.ReplacePattern(text, ch)  
  15. 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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.

 

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.

 

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.

VB.NET汉字转换的相关代码就为大家介绍到这里。

责任编辑:曹凯 来源: 博客园
相关推荐

2010-01-22 14:19:38

VB.NET调用jar

2009-10-28 09:55:29

VB.NET MyCl

2010-01-07 17:51:36

VB.NET实现Sin

2009-10-12 15:02:51

VB.NET动态控件

2010-01-21 16:45:00

VB.NET继承规则

2009-11-10 12:42:47

VB.NET Prin

2010-01-08 13:35:35

VB.NET写Log方

2010-01-08 15:03:12

VB.NET类属性

2009-11-10 12:48:17

VB.NET三维模型

2009-10-26 10:08:44

VB.NET工程转换成

2010-01-14 18:22:05

VB.NET硬盘速度测

2010-01-08 14:14:27

VB.NET使用Fin

2010-01-11 14:36:43

VB.NET安装部署

2010-01-13 16:15:47

VB.NET消息队列

2009-10-13 17:03:55

VB.NET面向对象

2009-11-02 15:45:03

VB.NET IEnu

2009-10-12 13:54:22

VB.NET Data

2010-01-07 15:18:10

VB.NET常量

2010-01-18 18:32:43

VB.NET监视启动过

2009-10-10 16:44:52

VB.NET开发控件
点赞
收藏

51CTO技术栈公众号