VB.NET中怎么实现汉字转换-成都快上网建站

VB.NET中怎么实现汉字转换

本篇文章给大家分享的是有关VB.NET中怎么实现汉字转换,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联建站-专业网站定制、快速模板网站建设、高性价比广饶网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式广饶网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖广饶地区。费用合理售后完善,十多年实体公司更值得信赖。

下面这段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中怎么实现汉字转换,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


名称栏目:VB.NET中怎么实现汉字转换
路径分享:http://kswjz.com/article/ipcjdg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流