3.搜尋字串

給出一個文字檔案 test.txt

Ford
Jeep
Honda

以下指令碼正在處理此文字檔案:

'Read in File Data to an array, separate by newline vb equivalent (vbcrlf)
Dim car, cars
Dim filefullname : filefullname = "C:\testenv\test.txt"
cars = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(filefullname, 1).ReadAll, vbcrlf)

'Exact Match search. 
Dim searchstring : searchstring = "Jeep"
For Each car In cars
    If Car = searchstring Then Exit For
Next

'Partial Match search
'Instr returns >0 if any result is found, if none, InStr returns -1
'The If statement will use -1 = false, >0 = true
Dim searchstring : searchstring = "Jee"
Dim position
For car = 0 To ubound(cars)
    If InStr(cars(car), searchstring) Then 
        position = car
        Exit For
    End If
Next