Find text in file, read to specific character is found

R4PM0NK3Y

Member
Joined
Dec 1, 2003
Messages
14
Hi, I need to do this:

First, open a text file. Read the file until it finds the phrase "test". Once found, it needs to read the text to the right of it into a string variable, until the character "<" is reached. How could I do this? Any help is greatly appreciated!
 
[VB]
declare a stream reader giving it the file name
Dim FileReader As New StreamReader("PathOfTheFile")
Dim FileContent As String = FileReader.ReadToEnd() reads the whole file into a string
Dim i As Integer = FileContent.IndexOf("test")
If i <> -1 Then
the file contains the word test
declare a string to hold the text from test to <
take a part of the string from where test is found + 4 (length of test)
Dim Result As String = FileContent.Substring(i + 4)
now find where < is found in this string
i = Result.IndexOf("<"c)
If i <> -1 Then
the result string contains <
take the string from the beginning till the last character (just before the <)
Result = Result.Substring(0, i - 1)
now result contains the string you want
End If
End If
[/VB]

Hope this helps,
 
That DID work (thanks a ton), but I made some modifications and have one more question.

In this line:

[VB]i = result.IndexOf("<"c)[/VB]

What does the "c" after "<" do? I need to change "<" to a variable name, and it seems the c is of importance, and I cannot have a variable name and the c in there. Example:

[VB]i = result.IndexOf(varc)[/VB]

Assume the variable used is "var".
What needs to be done? Thanks again.
 
In "<"c , the c simple tells the compiler to treat "<" as a character rather than a string. If you are using a variable then it isnt needed.
 
Back
Top