在 VBA 中检查一个字符串是否包含一个子字符串
作者:迹忆客
最近更新:2023/03/19
浏览次数:
本文将演示使用 Instr()
函数、InstrRev()
函数和 Like
函数来检查主字符串是否包含子字符串。
使用 Instr()
函数检查主字符串是否包含子字符串
Instr()
函数语法:
InStr([ start ], string1, string2, [ compare ])
返回类型:整数
参数:
[ start ] |
可选的。搜索将开始的数值。 对于 [ start ] 参数,以下是相应的值:1 - [默认] 搜索将从主字符串的开头开始< br/> n - 搜索将从 n 位置开始。 |
string1 |
强制的。要搜索的字符串(主字符串) |
string2 |
强制的。要查找的字符串。 |
[ compare ] |
可选的。指示将使用哪种字符串比较方法。 对于 [ compare ] 参数,以下是相应的值:0 - [默认] 二进制比较方法(区分大小写)1 - 文本比较方法(不区分大小写) |
下面的代码块将使用 Instr()
函数检查子字符串是否在 VBA 的主字符串中。
Function IsSubstring(pos as Integer, mainStr as String, subStr as String,compTyp as Integer) as boolean
'if `Instr()` function returned 0 then the substring is not present in the main string.
'If `Instr()` function returned a value greater than `0`, would mean that the substring is in the main string.
If Instr(pos,mainStr,subStr,compTyp) >0 Then
IsSubstring = true
Else: IsSubstring = false
End if
End Function
Sub test1()
Debug.print IsSubstring(1,"ABCDE","C",1)
End Sub
Sub test2()
Debug.print IsSubstring(1,"ABCDE","F",1)
End Sub
Sub test3()
Debug.print IsSubstring(1,"ABCDE","c",0)
End Sub
输出 test1
:
True
输出 test2
:
False
输出 test3
:
False
下面的代码块将使用 Instr()
函数从主字符串返回子字符串的位置。
Function GetPosition(pos as Integer, mainStr as String, subStr as String,compTyp as Integer)
'Check first if the substring is in the main string.
If InStr(pos, mainStr, subStr, compTyp) > 0 Then
'if substring is in the main string then get the position of the substring in the main string.
GetPosition = InStr(1, mainStr, subStr, 1)
Else: GetPosition = ("Subtring is not in the main string.")
End If
End Function
Sub test1()
'Check if `C` is in `ABCDE` starting at the first letter (A), case insensitive.
Debug.Print GetPosition(1,"ABCDE", "C",1)
End Sub
Sub test2()
'Check if `c` is in `ABCDE` starting at the first letter of the main string, case sensitive.
Debug.Print GetPosition(1,"ABCDE", "c",0)
End Sub
Sub test3()
'Check if `c` is in `ABCDE` starting at the fourth letter of the main string, case sensitive.
Debug.Print GetPosition(4,"ABCDE", "c",0)
End Sub
输出 test1
:
3
输出 test2
:
Subtring is not in the main string.
输出 test3
:
Subtring is not in the main string.
使用 InstrRev()
函数检查主字符串是否包含子字符串
InstrRev()
函数语法:
InStrRev(string1, string2,[ start ], [ compare ])
返回类型:整数
参数:
string1 |
强制的。要搜索的字符串(主字符串) |
string2 |
强制的。要查找的字符串。 |
下面的代码块将使用 InstrRev()
函数检查子字符串是否在 VBA 的主字符串中。
Function IsSubstring(mainStr As String, subStr As String) As Boolean
'if `InstrRev()` function returned 0 then the substring is not present in the main string.
'If `InstrRev()` function returned a value greater than `0`, would mean that the substring is in the main string.
If InStrRev(mainStr, subStr) > 0 Then
IsSubstring = True
Else: IsSubstring = False
End If
End Function
Sub test1()
Debug.Print IsSubstring("ABCDE", "C")
End Sub
Sub test2()
Debug.Print IsSubstring("ABCDE", "F")
End Sub
输出 test1
:
True
输出 test2
:
False
使用 Like
运算符检查主字符串是否包含子字符串
Like
运算符语法:
res = string Like pattern
返回类型:布尔值
参数:
res |
强制的。布尔返回值 |
string |
强制的。要查看的字符串 |
pattern |
强制的。要查找的字符串。更多详情见备注 |
备注:
? |
任何单个字符 |
* |
任意 0 到多个字符 |
# |
任何单个数字(0 到 9) |
下面的代码块将使用 Like
运算符检查子字符串是否在 VBA 的主字符串中
Function IsSubString(mainStr as String,subStr as String) as Boolean
'Check if subStr is in the main string by using *
If mainStr Like "*" & subStr & "*" Then
IsSubString = True
Else: IsSubstring= False
End If
End Function
Sub test1()
Debug.print (IsSubString("ABCDE","C"))
End Sub
Sub test2()
Debug.print (IsSubString("ABCDE","c"))
End Sub
Sub test3()
Debug.print (IsSubString("ABCDE","F"))
End Sub
输出 test1
:
True
输出 test2
:
False
输出 test3
:
False