Can't parse desired text from file after opening

N

.Net Sports

I'm trying to parse some text from an open file , and then save it and
display it, but with the code below, using two instances of IndexOf
doesnt work (error: length cannot be less than zero.), and using
LastIndexOf will grab the very last instance of my "secondkeyword",
which I don't want because there are more than one "secondkeyword"s
remaining in the file:


'''''''''''''''''''''''''''''
Dim mystring2 as string = String.Empty
Dim path2 as String = "c:\inetpub\wwwroot\myfile.txt"

If File.Exists(path2) Then
Dim sr2 As StreamReader = New StreamReader(path2)

mystring2 =sr2.ReadToEnd()
sr2.Close()

End If
Dim zz1 as String
zz1 = mystring2
Dim abc1 as String
Dim Ptr1 As Integer = zz1.IndexOf("firstkeyword")
Dim Ptr2 As Integer = zz1.IndexOf("secondkeyword")
abc1=zz1.Substring(Ptr1, Ptr2-Ptr1+1)
Response.Write(abc1)
'''''''''''''''''''''''''''''''''

??
chumley
 
G

Guest

You need to use an overload on the IndexOf to select a start id.

For instance:
Dim zz1 As String
Dim abc1 As String
Dim Ptr1 As Integer = zz1.IndexOf("firstkeyword")
Dim Ptr2 As Integer = zz1.IndexOf("secondkeyword", Ptr2)
abc1 = zz1.Substring(Ptr1, Ptr2 - Ptr1 + 1)

This way it prevents the 2nd placeholder from being less then the 1st ptr.

You also need to be verifying that Ptr2 is not greater then Ptr1, if it is,
then your parsing may need a bit of modification as it is not coming in as
you intended it to.
 
G

Guest

sorry about that, the overloaded line should look like this:
Dim Ptr2 As Integer = zz1.IndexOf("secondkeyword", Ptr1)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top