Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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!

Posted

'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

 

Hope this helps,

Dream as if you'll live forever, live as if you'll die today
Posted

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

 

In this line:

 

i = result.IndexOf("<"c)

 

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:

 

i = result.IndexOf(varc)

 

Assume the variable used is "var".

What needs to be done? Thanks again.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...