VB.NET汉字转换详细操作步骤介绍
创始人
2024-06-19 22:20:45
0

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 
  1. '全部替换当前文件的汉字  
  2. Public Sub 全部转换()  
  3. Dim doc As Document = 
    DTE.ActiveDocument  
  4. Dim docText As TextDocument = 
    doc.Object  
  5. Dim selText As TextSelection = 
    docText.Selection()  
  6. selText.SelectAll()  
  7. Dim text As String = selText.Text  
  8. Dim iLength As Integer  
  9. Do  
  10. iLength = text.Length  
  11. Dim m As Text.RegularExpressions.Match  
  12. '先找“字符串”:ChrW(12345) & 
    ChrW(23456) ... & ChrW(56789)  
  13. Dim strPattern As String = 
    "(ChrW\([0-9]{5}\)\s&\s)+ChrW\([0-9]{5}\)" 
  14. m = Regex.Match(text, strPattern, 
    RegexOptions.IgnoreCase)  
  15. If m.Success Then  
  16. Dim strMatch As String = m.Value  
  17. Dim strValue As String = m.Value  
  18. texttext = text.Replace(strValue, "")  
  19. strValuestrValue = strValue.
    Replace("&", "")  
  20. strValuestrValue = strValue.
    Replace(" ", "")  
  21. strValuestrValue = strValue.
    Replace("ChrW(", "")  
  22. strValuestrValue = strValue.
    Replace(")", "")  
  23. Dim chars As Integer = 
    strValue.Length()  
  24. charschars = chars \ 5  
  25. Dim strNew As String = "" 
  26. For i As Integer = 0 To chars - 1  
  27. Dim x As String = strValue.
    Substring(i * 5, 5)  
  28. strNewstrNew = strNew & ic(x)  
  29. Next  
  30. docText.ReplacePattern(strMatch, 
    """" & strNew & """")  
  31. Else  
  32. Exit Do  
  33. End If  
  34. If Not text.Length < iLength Then  
  35. Exit Do  
  36. End If  
  37. Loop 

 

  1. selText.SelectAll()  
  2. text = selText.Text  
  3. Do  
  4. iLength = text.Length  
  5. Dim m As Text.RegularExpressions.Match  
  6. '再找单字:" & ChrW(23456) & "  
  7. Dim strPattern As String = "\""\s&\
    sChrW\([0-9]{5}\)\s&\s\"""  
  8. m = Regex.Match(text, strPattern, 
    RegexOptions.IgnoreCase)  
  9. If m.Success Then  
  10. Dim strMatch As String = m.Value  
  11. Dim strValue As String = m.Value  
  12. texttext = text.Replace(strValue, "")  
  13. strValuestrValue = strValue.Replace
    ("&", "")  
  14. strValuestrValue = strValue.Replace
    (" ", "")  
  15. strValuestrValue = strValue.Replace
    ("ChrW(", "")  
  16. strValuestrValue = strValue.Replace
    (")", "")  
  17. strValuestrValue = strValue.Replace
    ("""", "")  
  18. Dim strNew = ic(strValue)  
  19. docText.ReplacePattern(strMatch, 
    strNew)  
  20. Else  
  21. Exit Do  
  22. End If  
  23. If Not text.Length < iLength Then  
  24. Exit Do  
  25. End If  
  26. Loop 

 

  1. selText.SelectAll()  
  2. text = selText.Text  
  3. Do  
  4. iLength = text.Length  
  5. Dim m As Text.RegularExpressions.Match  
  6. '再找单字:" & ChrW(23456)  
  7. Dim strPattern As String = "\""\s&\
    sChrW\([0-9]{5}\)" 
  8. m = Regex.Match(text, strPattern, 
    RegexOptions.IgnoreCase)  
  9. If m.Success Then  
  10. Dim strMatch As String = m.Value  
  11. Dim strValue As String = m.Value  
  12. texttext = text.Replace(strValue, "")  
  13. strValuestrValue = strValue.Replace
    ("&", "")  
  14. strValuestrValue = strValue.Replace
    (" ", "")  
  15. strValuestrValue = strValue.Replace
    ("ChrW(", "")  
  16. strValuestrValue = strValue.Replace
    (")", "")  
  17. strValuestrValue = strValue.Replace
    ("""", "")  
  18. Dim strNew = ic(strValue)  
  19. docText.ReplacePattern(strMatch, 
    strNew & """")  
  20. Else  
  21. Exit Do  
  22. End If  
  23. If Not text.Length < iLength Then  
  24. Exit Do  
  25. End If  
  26. Loop 

  1. selText.SelectAll()  
  2. text = selText.Text  
  3. Do  
  4. iLength = text.Length  
  5. Dim m As Text.Regular
    Expressions.Match  
  6. '再找单字:ChrW(23456) & "  
  7. Dim strPattern As String = 
    "ChrW\([0-9]{5}\)\s&\s\"""  
  8. m = Regex.Match(text, strPattern, 
    RegexOptions.IgnoreCase)  
  9. If m.Success Then  
  10. Dim strMatch As String = m.Value  
  11. Dim strValue As String = m.Value  
  12. texttext = text.Replace(strValue, "")  
  13. strValuestrValue = strValue.
    Replace("&", "")  
  14. strValuestrValue = strValue.
    Replace(" ", "")  
  15. strValuestrValue = strValue.
    Replace("ChrW(", "")  
  16. strValuestrValue = strValue.
    Replace(")", "")  
  17. strValuestrValue = strValue.
    Replace("""", "")  
  18. Dim strNew = ic(strValue)  
  19. docText.ReplacePattern(strMatch, 
    """" & strNew)  
  20. Else  
  21. Exit Do  
  22. End If  
  23. If Not text.Length < iLength Then  
  24. Exit Do  
  25. End If  
  26. Loop  
  27. selText.SelectAll()  
  28. text = selText.Text  
  29. Do  
  30. iLength = text.Length  
  31. Dim m As Text.RegularExpressions.
    Match  
  32. '***单字:ChrW(23456)  
  33. Dim strPattern As String = 
    "ChrW\([0-9]{5}\)" 
  34. m = Regex.Match(text, strPattern, 
    RegexOptions.IgnoreCase)  
  35. If m.Success Then  
  36. Dim strMatch As String = m.Value  
  37. Dim strValue As String = m.Value  
  38. texttext = text.Replace(strValue, "")  
  39. strValuestrValue = strValue.
    Replace(" ", "")  
  40. strValuestrValue = strValue.
    Replace("ChrW(", "")  
  41. strValuestrValue = strValue.
    Replace(")", "")  
  42. Dim strNew = ic(strValue)  
  43. docText.ReplacePattern
    (strMatch, """" & strNew & """")  
  44. Else  
  45. Exit Do  
  46. End If  
  47. If Not text.Length < iLength Then  
  48. Exit Do  
  49. End If  
  50. Loop  
  51. End Sub  
  52. Private Function cc(ByVal 
    str As String) As Char  
  53. Dim int As Integer = CInt
    (str.Substring(5, 5))  
  54. Dim ch As Char = ChrW(int)  
  55. Return ch  
  56. End Function  
  57. Private Function ic(ByVal 
    int As Integer) As Char  
  58. Dim ch As Char = ChrW(int)  
  59. Return ch  
  60. End Function  
  61. End Module 

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

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...