VB6 中有 Like 运算符。它是一个非常实用的字符串模式匹配工具,用于判断一个字符串是否符合指定的模式。
Like 运算符返回一个布尔值 (True 或 False)。
1. 基本语法
result = string Like pattern
result: 一个布尔变量,用于存储比较结果。如果 string 匹配 pattern,则 result 为 True,否则为 False。
string: 要进行匹配的字符串表达式。
pattern: 一个包含通配符的字符串,用于定义匹配规则。
2. 通配符 (Wildcards)
Like 的强大之处在于其模式中可以使用通配符。下表是 VB6 支持的通配符及其含义:
通配符 描述 示例 (pattern) 匹配的字符串 (string) 不匹配的字符串 (string)
* 匹配零个或多个任意字符。 "A*" "A", "Apple", "A123" "Ba", "banana"
? 匹配一个任意字符。 "A?C" "ABC", "AXC", "A1C" "AC", "ABCD"
# 匹配一个数字 (0-9)。 "12#4" "1234", "1254", "1294" "12a4", "12345"
[charlist] 匹配括号中指定的任意一个字符。 "[AEIOU]" "A", "E", "I" "B", "a" (取决于比较模式)
[!charlist] 匹配任何不在括号中指定的字符。 "[!0-9]" "A", "!", "b" "0", "5"
- 在 [] 中表示一个字符范围。 "[A-Z]" "B", "K", "Z" "a", "1"
! 在 [] 中表示非或排除。 "[!A-Z]" "a", "1", "!" "B", "Z"
注意: 如果你想匹配通配符本身(例如,你想查找包含星号 * 的字符串),你需要用方括号 [] 将其括起来。例如,模式 "a[*]b" 会匹配 "a*b"。
3. 使用示例
下面是一些在 VB6 代码中使用 Like 的具体例子。
示例 1:匹配任意长度的字符串
检查一个文件名是否以 ".txt" 结尾。
Dim fileName As String
Dim isTextFile As Boolean
fileName = "report.txt"
isTextFile = fileName Like "*.txt"
' isTextFile 的值将是 True
MsgBox """" & fileName & """" & IIf(isTextFile, " 是 ", " 不是 ") & "一个文本文件。"
fileName = "image.jpg"
isTextFile = fileName Like "*.txt"
' isTextFile 的值将是 False
MsgBox """" & fileName & """" & IIf(isTextFile, " 是 ", " 不是 ") & "一个文本文件。"
检查一个 3 位数的数字,其中第一位是 1,最后一位是 5。
Dim numberStr As String
Dim isMatch As Boolean
numberStr = "125"
isMatch = numberStr Like "1?5"
' isMatch 的值将是 True
MsgBox """" & numberStr & """" & IIf(isMatch, " 匹配 ", " 不匹配 ") & "模式 ""1?5""。"
numberStr = "1225"
isMatch = numberStr Like "1?5"
' isMatch 的值将是 False (因为长度不符)
MsgBox """" & numberStr & """" & IIf(isMatch, " 匹配 ", " 不匹配 ") & "模式 ""1?5""。"
检查一个字符串是否是一个 4 位数字。
Dim inputStr As String
Dim isFourDigit As Boolean
inputStr = "1987"
isFourDigit = inputStr Like "####"
' isFourDigit 的值将是 True
MsgBox """" & inputStr & """" & IIf(isFourDigit, " 是 ", " 不是 ") & "一个4位数字。"
inputStr = "19A7"
isFourDigit = inputStr Like "####"
' isFourDigit 的值将是 False
MsgBox """" & inputStr & """" & IIf(isFourDigit, " 是 ", " 不是 ") & "一个4位数字。"
检查一个字符是否是元音字母(不区分大小写)。
Dim char As String
Dim isVowel As Boolean
char = "E"
' [AEIOUaeiou] 匹配任何大小写的元音字母
isVowel = char Like "[AEIOUaeiou]"
' isVowel 的值将是 True
MsgBox "字符 """ & char & """" & IIf(isVowel, " 是 ", " 不是 ") & "一个元音字母。"
char = "B"
isVowel = char Like "[AEIOUaeiou]"
' isVowel 的值将是 False
MsgBox "字符 """ & char & """" & IIf(isVowel, " 是 ", " 不是 ") & "一个元音字母。"
检查一个字符串是否以字母开头。
Dim name As String
Dim startsWithLetter As Boolean
name = "Alice"
startsWithLetter = name Like "[A-Za-z]*"
' startsWithLetter 的值将是 True
MsgBox """" & name & """" & IIf(startsWithLetter, " 以 ", " 不以 ") & "字母开头。"
Like 常用于 If 语句的条件判断。
Dim userInput As String
userInput = InputBox("请输入你的年龄 (一个数字):")
If userInput Like "#*" Then ' 检查第一个字符是否是数字
MsgBox "你输入的是: " & userInput
Else
MsgBox "无效输入!请输入一个数字。"
End If
Like 运算符的比较行为受模块顶部的 Option Compare 语句影响:
Option Compare Binary (默认): 进行区分大小写的比较。例如,"A" Like "[a-z]" 会返回 False。
Option Compare Text: 进行不区分大小写的比较。例如,"A" Like "[a-z]" 会返回 True。
因此,在编写需要匹配大小写的代码时,请务必注意模块开头的 Option Compare 设置。
总结
Like 是 VB6 中一个功能强大且易于使用的字符串处理工具,特别适合用于简单的模式验证、数据筛选和文本搜索等场景。通过灵活运用各种通配符,可以轻松实现复杂的匹配逻辑。
如果:
Private Sub Command1_Click()
a = "<!--#i"
b = "*[<!--#i]*"
Debug.Print a Like b
End Sub
就出错,这样就不出错:
Private Sub Command1_Click()
a = "<!--#i"
b = "*<!--#i"
Debug.Print a Like b
End Sub