从字符串中去除汉字 (vba)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10710518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:15:04  来源:igfitidea点击:

Strip Chinese Characters from a string (vba)

regexvbams-office

提问by JimS-CLT

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

我正在使用 Microsoft Project VBA 将我的活动名称从英文翻译成中文。

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.

我的问题是我在一些英文活动名称中嵌入了一些中文翻译。我想在将字符串传递给 Microsoft Translator 之前去除汉字。

Any ideas as to how I can do that?

关于我如何做到这一点的任何想法?

回答by brettdj

You can use a Regexpto strip the Chinese unicode characters

您可以使用 aRegexp来剥离中文 unicode 字符

Wikipedialists the relevant characters below

维基百科列出了下面的相关字符

enter image description here

在此处输入图片说明

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'sstring example

所以这个正则表达式会去掉这些范围。我使用过aldo.roman.nurena 的字符串示例

回答by aldo.roman.nurena

You have to use ChrW$()as this:

你必须这样使用ChrW$()

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

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

H6C49可用(感谢上帝为)上的Unicode中日韩码( CN ,日本和韩国)。请参阅此内容以查看字符范围。

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.

因此,您必须检查字符 Unicode 代码,然后比较它是否已经在 CJK 范围内,以便对其进行翻译。

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

还有一个很好的解释,甚至一个程序字符串翻译在这里