473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Jan 20 '06 #1
2 1013
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

Jan 20 '06 #2
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

Jan 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lostinspace | last post by:
After four+ years of using FrontPage in a limited capacity and having created over 600 pages on my sites, I've finally (at least for the most part) abandoned FP, to begin the process of converting...
15
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the...
59
by: Michael C | last post by:
eg void DoIt() { int i = FromString("1"); double d = FromString("1.1"); } int FromString(string SomeValue) {
1
by: Stephane | last post by:
Hi, I have a html file file that I want to parse with ASP.NET to retreive the value of a custom tag. Let's say that the average html file is about 30 ko. Once the html file is loaded and...
6
by: slacker | last post by:
In my jsp I have: <Script langauge="Javascript"> window.location.replace("first url"); window.close(); window.open("second url"); </Script> The child window opens from the parent window...
3
by: Sam | last post by:
Hi Everybody, I'm using C# and System.XML and can parse XML files like this: <LogIn>Yes</LogIn> <Name>DB_MAGUser</Name> but I've been asked to parse and work with a XML file that looks...
13
by: sonald | last post by:
Hi, Can anybody tell me how to change the text delimiter in FastCSV Parser ? By default the text delimiter is double quotes(") I want to change it to anything else... say a pipe (|).. can anyone...
3
by: rajmohan.h | last post by:
Hi all, Suppose I have a string which contains quotes inside quotes - single and double quotes interchangeably - s = "a1' b1 " c1' d1 ' c2" b2 'a2" I need to start at b1 and end at b2 - i.e. I...
3
by: jackson.rayne | last post by:
Hello, Another newbie question here. Let me explain my situation first. I have bought a 3rd party tool that runs a PHP script and gives me some HTML code which I can directly use in my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.