Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Can't parse desired text from file after opening

Reply
Thread Tools

Can't parse desired text from file after opening

 
 
.Net Sports
Guest
Posts: n/a
 
      01-20-2006
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIEFuZGVyc29u?=
Guest
Posts: n/a
 
      01-20-2006
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
>
>

 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIEFuZGVyc29u?=
Guest
Posts: n/a
 
      01-20-2006
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
> >
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with opening Excel after opening Excel inside Microsoft Internet Controls fniles ASP General 0 04-26-2009 09:39 PM
Word file not opening from Adm -My recent docs but opening from file menu of Word Thaqalain Computer Support 0 06-30-2005 02:20 AM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM
How to check for EOF (End of file) when using StreamReader to parse text file Sacha Korell ASP .Net 2 09-06-2003 02:59 PM
Parse Text File and Output to File John M. Lembo Perl 0 08-01-2003 04:34 PM



Advertisments