溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

VB.NET中怎么實現(xiàn)漢字轉(zhuǎn)換

發(fā)布時間:2021-07-15 11:37:28 來源:億速云 閱讀:149 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關(guān)VB.NET中怎么實現(xiàn)漢字轉(zhuǎn)換,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

下面這段VB.NET漢字轉(zhuǎn)換代碼解決了這個問題,將其拷到宏編輯器里保存即可,寫的有點傻傻的,懶得優(yōu)化了,好使就行:)

  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 單字轉(zhuǎn)換()  

  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 全部轉(zhuǎn)換()  

  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中怎么實現(xiàn)漢字轉(zhuǎn)換,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI