我想要最快的方法(在 excel VBA 中)来识别一个字符串是否出现在另一个字符串中的任何地方 - 也许是“包含”函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1099479/
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 10:33:37  来源:igfitidea点击:

I'd like the quickest way (in excel VBA) to identify whether one string occurs anywhere within another - an 'includes' function perhaps?

stringexcelvba

提问by Carl Manaster

I'd like the quickest way (in excel VBA) to identify whether one string occurs anywhere within another - an 'includes' function perhaps?

我想要最快的方法(在 excel VBA 中)来识别一个字符串是否出现在另一个字符串中的任何地方 - 也许是“包含”函数?

回答by Carl Manaster

I believe INSTR()is the function; if it returns anything other than zero, the string is found.

我相信INSTR()是函数;如果它返回零以外的任何内容,则找到该字符串。

exists = InStr("avbprogram", "vb") <> 0

回答by Carl Manaster

Carl is correct but you should also know that the default compare option for InStr is case-sensitive. If you want to do case-insensitive checks you should either wrap your arguments in LCase/UCase or use the extended form of the InStr function as shown below:

Carl 是正确的,但您还应该知道 InStr 的默认比较选项区分大小写。如果您想进行不区分大小写的检查,您应该将参数包装在 LCase/UCase 中或使用 InStr 函数的扩展形式,如下所示:

exists = InStr(1, "avbprogram", "vb", vbTextCompare)

where the first argument is the index of the first character to start comparing from and the last argument indicates case-insensitive comparison. The short-hand that Carl showed is actually equivalent to what is shown below:

其中第一个参数是开始比较的第一个字符的索引,最后一个参数表示不区分大小写的比较。Carl 展示的简写实际上等同于下图所示的内容:

exists = InStr(1, "avbprogram", "vb", vbBinaryCompare)

回答by puzzlepiece87

Another option is using the Likeoperator.

另一种选择是使用Like运算符。

The following usage guidance from MSDNis helpful:

以下来自MSDN 的使用指南很有帮助:

Dim testCheck As Boolean
' The following statement returns True (does "F" satisfy "F"?)
testCheck = "F" Like "F"
' The following statement returns False for Option Compare Binary
'    and True for Option Compare Text (does "F" satisfy "f"?)
testCheck = "F" Like "f"
' The following statement returns False (does "F" satisfy "FFF"?)
testCheck = "F" Like "FFF"
' The following statement returns True (does "aBBBa" have an "a" at the
'    beginning, an "a" at the end, and any number of characters in 
'    between?)
testCheck = "aBBBa" Like "a*a"
' The following statement returns True (does "F" occur in the set of
'    characters from "A" through "Z"?)
testCheck = "F" Like "[A-Z]"
' The following statement returns False (does "F" NOT occur in the 
'    set of characters from "A" through "Z"?)
testCheck = "F" Like "[!A-Z]"
' The following statement returns True (does "a2a" begin and end with
'    an "a" and have any single-digit number in between?)
testCheck = "a2a" Like "a#a"
' The following statement returns True (does "aM5b" begin with an "a",
'    followed by any character from the set "L" through "P", followed
'    by any single-digit number, and end with any character NOT in
'    the character set "c" through "e"?)
testCheck = "aM5b" Like "a[L-P]#[!c-e]"
' The following statement returns True (does "BAT123khg" begin with a
'    "B", followed by any single character, followed by a "T", and end
'    with zero or more characters of any type?)
testCheck = "BAT123khg" Like "B?T*"
' The following statement returns False (does "CAT123khg"?) begin with
'    a "B", followed by any single character, followed by a "T", and
'    end with zero or more characters of any type?)
testCheck = "CAT123khg" Like "B?T*"