In file parsing, taking the first few characters of a text file after a readfile or streamreader fil

Discussion in 'ASP .Net' started by .Net Sports, Jan 16, 2006.

  1. .Net Sports

    .Net Sports Guest

    In VB.net, I'm trying to do a couple of things in a couple of different
    blocks of code. I need to take the first 25 characters of a text file,
    then append at the end some ellipses and a MORE link to a webpage where
    viewers can read the rest of the article:
    This is the first few characters of text from my file.......<a
    href="article-to-read.aspx"> MORE </a>
    ...I also need to do some in file parsing where I start at one known
    keyword (START for an example) , grab all the text until , let's say
    the next </td>, put it all into a variable, and then be able to use
    that variable to display the results of all the text from START to
    </td> . IndexOf seems like the builtin function I may need to use, but
    having difficulty finding methods and arguments to do in file searching
    for this.

    thanks
    netsports
    .Net Sports, Jan 16, 2006
    #1
    1. Advertising

  2. RE: In file parsing, taking the first few characters of a text file af

    Sorry,
    that was C# (I'm VB.NET - challenged) but you get the idea.
    Peter

    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    ".Net Sports" wrote:

    > In VB.net, I'm trying to do a couple of things in a couple of different
    > blocks of code. I need to take the first 25 characters of a text file,
    > then append at the end some ellipses and a MORE link to a webpage where
    > viewers can read the rest of the article:
    > This is the first few characters of text from my file.......<a
    > href="article-to-read.aspx"> MORE </a>
    > ...I also need to do some in file parsing where I start at one known
    > keyword (START for an example) , grab all the text until , let's say
    > the next </td>, put it all into a variable, and then be able to use
    > that variable to display the results of all the text from START to
    > </td> . IndexOf seems like the builtin function I may need to use, but
    > having difficulty finding methods and arguments to do in file searching
    > for this.
    >
    > thanks
    > netsports
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jan 16, 2006
    #2
    1. Advertising

  3. RE: In file parsing, taking the first few characters of a text file af

    You want to use the Substring method, combined with IndexOf and related for
    stuff like this.

    string newStr =MyText.Substring(0, 25)
    or
    string newStr=MyText.Substring(0, MyText.LastIndexOf("<BR>");


    Peter
    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    ".Net Sports" wrote:

    > In VB.net, I'm trying to do a couple of things in a couple of different
    > blocks of code. I need to take the first 25 characters of a text file,
    > then append at the end some ellipses and a MORE link to a webpage where
    > viewers can read the rest of the article:
    > This is the first few characters of text from my file.......<a
    > href="article-to-read.aspx"> MORE </a>
    > ...I also need to do some in file parsing where I start at one known
    > keyword (START for an example) , grab all the text until , let's say
    > the next </td>, put it all into a variable, and then be able to use
    > that variable to display the results of all the text from START to
    > </td> . IndexOf seems like the builtin function I may need to use, but
    > having difficulty finding methods and arguments to do in file searching
    > for this.
    >
    > thanks
    > netsports
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jan 16, 2006
    #3
  4. .Net Sports

    .Net Sports Guest

    Re: In file parsing, taking the first few characters of a text file af

    Thanks....

    .....can anyone provide a vb.net equivalent?
    .Net Sports, Jan 16, 2006
    #4
  5. Re: In file parsing, taking the first few characters of a text file af

    The VB.Net equivalent of:

    string newStr =MyText.Substring(0, 25)
    or
    string newStr=MyText.Substring(0, MyText.LastIndexOf("<BR>");

    is:

    Dim newStr As String =MyText.Substring(0, 25)
    or
    Dim newStr As String=MyText.Substring(0, MyText.LastIndexOf("<BR>")

    ......

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    ".Net Sports" <> wrote in message
    news:...
    > Thanks....
    >
    > ....can anyone provide a vb.net equivalent?
    >
    Kevin Spencer, Jan 16, 2006
    #5
  6. =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jan 16, 2006
    #6
  7. .Net Sports

    .Net Sports Guest

    Re: In file parsing, taking the first few characters of a text file af

    Thanks for the input (and converter, Peter)..
    ..I'm getting a substring not a member of 'System.IO.StreamReader'
    error when trying this. And also, I'm not using any type of input
    control (for the MyText. prefix)
    thanks
    netsports

    Kevin Spencer wrote:
    > The VB.Net equivalent of:
    >
    > string newStr =MyText.Substring(0, 25)
    > or
    > string newStr=MyText.Substring(0, MyText.LastIndexOf("<BR>");
    >
    > is:
    >
    > Dim newStr As String =MyText.Substring(0, 25)
    > or
    > Dim newStr As String=MyText.Substring(0, MyText.LastIndexOf("<BR>")
    >
    > .....
    >
    > Kevin Spencer
    > Microsoft MVP
    > .Net Developer
    > You can lead a fish to a bicycle,
    > but it takes a very long time,
    > and the bicycle has to *want* to change.
    >
    > ".Net Sports" <> wrote in message
    > news:...
    > > Thanks....
    > >
    > > ....can anyone provide a vb.net equivalent?
    > >
    .Net Sports, Jan 16, 2006
    #7
  8. Re: In file parsing, taking the first few characters of a text fil

    Ok, well you'll need to post some sample code so the incredible cadre of
    helpful experts here can show how to fix it. Substring is a method on the
    string class, has nothing to do with StreamReader.

    Example:

    Dim xyz as String
    xyz="12345678901234567890"
    Dim abc as String = xyz.Substring(0,9)

    --Peter

    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    ".Net Sports" wrote:

    > Thanks for the input (and converter, Peter)..
    > ..I'm getting a substring not a member of 'System.IO.StreamReader'
    > error when trying this. And also, I'm not using any type of input
    > control (for the MyText. prefix)
    > thanks
    > netsports
    >
    > Kevin Spencer wrote:
    > > The VB.Net equivalent of:
    > >
    > > string newStr =MyText.Substring(0, 25)
    > > or
    > > string newStr=MyText.Substring(0, MyText.LastIndexOf("<BR>");
    > >
    > > is:
    > >
    > > Dim newStr As String =MyText.Substring(0, 25)
    > > or
    > > Dim newStr As String=MyText.Substring(0, MyText.LastIndexOf("<BR>")
    > >
    > > .....
    > >
    > > Kevin Spencer
    > > Microsoft MVP
    > > .Net Developer
    > > You can lead a fish to a bicycle,
    > > but it takes a very long time,
    > > and the bicycle has to *want* to change.
    > >
    > > ".Net Sports" <> wrote in message
    > > news:...
    > > > Thanks....
    > > >
    > > > ....can anyone provide a vb.net equivalent?
    > > >

    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jan 16, 2006
    #8
  9. .Net Sports

    .Net Sports Guest

    Re: In file parsing, taking the first few characters of a text fil

    <%Dim mystring As StreamReader =
    File.OpenText("c:\inetpub\wwwroot\articles\thefile.txt")
    If File.Exists("c:\inetpub\wwwroot\articles\thefile.txt") = True Then
    Do While mystring.Peek() >= 0
    dim firsttext as string =Substring(mystring(0,25))
    response.write(firsttext.ReadLine())
    Loop
    mystring.Close()
    End If
    .Net Sports, Jan 16, 2006
    #9
  10. Re: In file parsing, taking the first few characters of a text fil

    OK. Lets refactor that a bit and clean it up:


    Dim mystring as as string = String.Empty
    Dim path as String = "c:\inetpub\wwwroot\articles\thefile.txt"
    Try
    If File.Exists(path) Then
    Dim sr As StreamReader = New StreamReader(path)
    'This allows you to do one Read operation.
    mystring =sr.ReadToEnd()
    sr.Close()
    Catch e As Exception
    ' handle the exception
    Response.Write ("The process failed: {0}", e.ToString())
    End Try
    End If
    ' on mystring, which now holds the entire file contents, you can apply the
    Substring method as described earlier.

    Hope that helps.


    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    ".Net Sports" wrote:

    > <%Dim mystring As StreamReader =
    > File.OpenText("c:\inetpub\wwwroot\articles\thefile.txt")
    > If File.Exists("c:\inetpub\wwwroot\articles\thefile.txt") = True Then
    > Do While mystring.Peek() >= 0
    > dim firsttext as string =Substring(mystring(0,25))
    > response.write(firsttext.ReadLine())
    > Loop
    > mystring.Close()
    > End If
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jan 17, 2006
    #10
  11. Re: In file parsing, taking the first few characters of a text fil

    And kindly note that as I explained, I obviously remain quite VB.NET
    challenged:
    the last two lines:
    End Try
    End If
    should be reversed:

    End If
    End Try

    Have you ever thought about C#? :)
    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    ".Net Sports" wrote:

    > <%Dim mystring As StreamReader =
    > File.OpenText("c:\inetpub\wwwroot\articles\thefile.txt")
    > If File.Exists("c:\inetpub\wwwroot\articles\thefile.txt") = True Then
    > Do While mystring.Peek() >= 0
    > dim firsttext as string =Substring(mystring(0,25))
    > response.write(firsttext.ReadLine())
    > Loop
    > mystring.Close()
    > End If
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jan 17, 2006
    #11
  12. .Net Sports

    .Net Sports Guest

    Re: In file parsing, taking the first few characters of a text fil

    Thanks a bunch for the help, looks like this is getting me on my way.
    Yep, I do need to develop C# skills, but I do need more an overall
    grasp of .net technologies at the same time.
    netsports
    .Net Sports, Jan 17, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sacha Korell
    Replies:
    2
    Views:
    14,854
    Mattias Sjögren
    Sep 6, 2003
  2. Max
    Replies:
    4
    Views:
    8,926
    David Browne
    Nov 18, 2004
  3. .Net Sports
    Replies:
    0
    Views:
    466
    .Net Sports
    Jan 11, 2006
  4. damezumari
    Replies:
    2
    Views:
    489
    damezumari
    Apr 27, 2006
  5. Murali
    Replies:
    2
    Views:
    539
    Jerry Coffin
    Mar 9, 2006
Loading...

Share This Page