arm*_*rhb 120
做这件事有很多种方法:
您可以使用InStr
内置函数来测试String是否包含子字符串. InStr
将返回第一个匹配的索引,或0.因此,您可以通过执行以下操作来测试String是否以子字符串开头:
If InStr(1, "Hello World", "Hello W") = 1 Then
MsgBox "Yep, this string begins with Hello W!"
End If
Run Code Online (Sandbox Code Playgroud)
如果InStr
返回1
,则String("Hello World")以子字符串("Hello W")开头.
您还可以使用like
比较运算符以及一些基本模式匹配:
If "Hello World" Like "Hello W*" Then
MsgBox "Yep, this string begins with Hello W!"
End If
Run Code Online (Sandbox Code Playgroud)
在这里,我们使用星号(*)来测试String是否以我们的子字符串开头.
Bla*_*awk 35
根据Java函数的声明和描述来startsWith
判断,在VBA中实现它的"最直接的方法"是Left
:
Public Function startsWith(str As String, prefix As String) As Boolean
startsWith = Left(str, Len(prefix)) = prefix
End Function
Run Code Online (Sandbox Code Playgroud)
或者,如果您希望偏移参数可用,请使用Mid
:
Public Function startsWith(str As String, prefix As String, Optional toffset As Integer = 0) As Boolean
startsWith = Mid(str, toffset + 1, Len(prefix)) = prefix
End Function
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
119613 次 |
最近记录: |