-
Finding a string in text
The way I've been finding a string in text is a slow way.
Here's how I would:
For I = 1 to Len(rtbFile.txt)
If Mid(rtbFile.txt, I, 9) = "Something" then
'Found Text
End if
Next I
I know this a slower way than normal. How would I find a string faster than using this method?
Thank you for any help
-Justin Gomes
[email protected]
-
Code:
Dim FileName As String
FileName = "rtbFile.txt"
Dim strTemp As String
Open FileName For Input As #1
strTemp = Input(LOF(1),1)
Close #1
If InStr(1,strTemp,"Something") <> 0 Then
MsgBox "String Found!"
End If
-
Use the Like operator.
Code:
If rtbFile.txt Like "*Something*" Then Msgbox "Something found!"
-
'match case
If InStr(1,strTemp,"Something",vbBinaryCompare) <> 0 Then
MsgBox "String Found!"
End If
'no match case
If InStr(1,strTemp,"Something",vbTextCompare) <> 0 Then
MsgBox "String Found!"
End If
for a good tool for programmers.
please visit: http://www.iCodeRepository.com
-
I'm sorry
I am sorry I wasn't detailed enough.
After finding the string I need to know where the string is inside the file.
For example, the len of the string location
-
Code:
Dim iPos As Integer
Dim FileName As String
FileName = "rtbFile.txt"
Dim strTemp As String
Open FileName For Input As #1
strTemp = Input(LOF(1),1)
Close #1
iPos = InStr(1,strTemp,"Something")
If iPos <> 0 Then Msgbox "Something found at position: " & iPos
-
<?>
Code:
'open a file and find the first occurance of a string and return the
'occurance and the remainder of the file
'remove option compare text if you want the seach to be case aware
Option Explicit
Option Compare Text
'contents of file
'I was drinking tea in the beverage room of the house
Private Sub Command1_Click()
Dim sfile As String, intNum As Integer, myString As String
intNum = FreeFile
sfile = "C:\my Documents\myfile.txt"
Open sfile For Input As intNum
myString = StrConv(InputB(LOF(intNum), intNum), vbUnicode)
Close intNum
Dim myFind As String, myLen As String, iCount As Integer
Dim mySearch As String
mySearch = "the"
For iCount = 1 To Len(myString)
myLen = InStr(1, myString, mySearch)
Next
'the balance of the file including "the first" [the]
'in this case [the beverage room of the house
myString = Right(myString, Len(myString) - (CInt(myLen) - 1))
MsgBox myString 'result of search
End Sub
[/code]