Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Can't parse desired text from file after opening (http://www.velocityreviews.com/forums/t117901-cant-parse-desired-text-from-file-after-opening.html)

.Net Sports 01-20-2006 09:48 PM

Can't parse desired text from file after opening
 
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


=?Utf-8?B?VG9tIEFuZGVyc29u?= 01-20-2006 09:58 PM

RE: Can't parse desired text from file after opening
 
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.

".Net Sports" wrote:

> 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
>
>


=?Utf-8?B?VG9tIEFuZGVyc29u?= 01-20-2006 10:00 PM

RE: Can't parse desired text from file after opening
 
sorry about that, the overloaded line should look like this:
Dim Ptr2 As Integer = zz1.IndexOf("secondkeyword", Ptr1)

"Tom Anderson" wrote:

> 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.
>
> ".Net Sports" wrote:
>
> > 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
> >
> >



All times are GMT. The time now is 01:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.