我想在vb6的文件中搜索多个字符串 使用 instr 我们可以为单个字符串做但我不知道如何使用instr多个字符串现在如何搜索多个字符串如果找到其中一个我们收到一条消息?
Open file For Binary As #1
strData = Space$(FileLen(file))
Get #1, , strData
Close #1
lngFind = InStr(1, strData, string)
答案 0 :(得分:2)
这只是为多个字符串引入多个测试的情况......
Dim strArray(10) As String
DIm cntArray(10) As Integer
Dim strData As String
Dim c As Integer
'Set-up your search strings...
...
Open file For Binary As #1
Get #1, , strData
Close #1
For c = 1 to 10
cntArray(c) = Instr(strData, strArray(c))
Next c
如果你要做的只是显示一个真或假的消息框,那么我们不需要将值分配给第二个数组。 For
循环可以替换为...
For c = 1 to 10
If Instr(strData, strArray(c)) > 0 Then
MsgBox "'" & strArray(c) & "' found in file."
'Remove the following line if you want everything to be searched for,
'but leave it in if you only want the first string found...
Exit For
End If
Next c
真的,这是一段非常基本的代码。如果您希望将代码编写为除了新手之外的任何东西,那么您需要研究本文中包含的命令,函数和结构。对于一个完整的新手来说,一个好的起点可能是http://www.thevbprogrammer.com/classic_vbtutorials.asp或http://www.vb6.us/。
答案 1 :(得分:0)
'-----------------------------------------------------------
'perform multiple instr on a string. returns true if all instr pass
'-----------------------------------------------------------
Function bMultiInstr(sToInspect As String, ParamArray sArrConditions()) As Boolean
On Error GoTo err:
Dim i As Integer, iUpp As Integer
iUpp = UBound(sArrConditions) 'instr conditions
For i = 0 To iUpp ' loop them
If InStr(1, sToInspect, sArrConditions(i)) <= 0 Then Exit Function ' if instr returns 0 then exit - [bPasses] will be left false
Next i
bPasses = True
Exit Function
err:
With err
If .Number <> 0 Then
'create .bas named [ErrHandler] see http://vb6.info/h764u
ErrHandler.ReportError Date & ": Strings.bMultiInstr." & err.Number & "." & err.Description
Resume Next
End If
End With
End Function
来自http://vb6.info/string/instr-multi-perform-instr-checks-multiple-inst-conditions-function/