Strip Chinese Characters from a string (vba)

戏子无情 提交于 2019-12-19 04:44:07

问题


I am using Microsoft Project VBA to translate my activity names from English to Chinese.

My problem is I have some Chinese translations embedded in some of the English activity names. I want to strip out the Chinese characters before passing the string to Microsoft Translator.

Any ideas as to how I can do that?


回答1:


You can use a Regexp to strip the Chinese unicode characters

Wikipedia lists the relevant characters below

Sub Test()
Dim myString as String
myString = "This is my string with a " & ChrW$(&H6C49) & " in it."
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Global = True
    .Pattern = "[\u4E00-\u9FFF\u6300-\u77FF\u7800-\u8CFF\u8D00-\u9FFF]+"
    MsgBox .Replace(myString, vbNullString)
End With
End Sub

So this regexp will strip out these ranges. I have used aldo.roman.nurena's string example




回答2:


You have to use ChrW$() as this:

MyString = "This is my string with a " & ChrW$(&H6C49) & " in it."

The H6C49 is available (thanks God for that) on Unicode as CJK codes (Chinese, Japanese and Korean). See this to take a look of the characters range.

So, you have to check the character Unicode code and then compare if it is already on the CJK range so as to translate it or not.

There is also a good explanation and even a program to translate strings here



来源:https://stackoverflow.com/questions/10710518/strip-chinese-characters-from-a-string-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!