Results 1 to 6 of 6

Thread: Parse a text file by line

  1. #1

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Parse a text file by line

    I'm running into a problem whenever I try to parse a text file by each line. I know I could use stream reader to read line by line but it is a lot easier to simply use split() and I would also like to know the reason why split() doesn't work.

    For example, I created a file "test.txt" and filled it with the following text.

    Code:
    text1
    text2
    text3
    then put the following code in the load event of the form (a button click would work the same).

    Code:
            Dim str As String = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\test.txt")
            MsgBox("testforline" & str.Split(Environment.NewLine)(1))
    The result is text2 appearing on the line below testforline. When using split, split is supposed to remove the character that was dividing the information as well however this isn't the case with new lines. How can I fix this? Also I tried using cchar() on environment.newline and controlchars.newline and neither worked.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Parse a text file by line

    The reason that Split doesn't work is that the overload you're using splits on a Char, i.e. a single character, not on a String. Environment.NewLine is a String that contains two characters, i.e. CR and LF. In your code, that is being implicitly converted to a Char, i.e. CR, so the LF gets left behind in the data.

    First, you must have Option Strict Off or that code wouldn't even compile. Turn Option Strict On and leave it on and then you won;t ever be surprised by implicit conversions like that.

    There is an overload of String.Split that lets you split on substrings rather than characters, but you don;t need to use it anyway. The easy option here is:
    vb.net Code:
    1. For Each line As String In IO.File.ReadAllLines("file path here")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Parse a text file by line

    I always use option strict on but I put together this example rather than the exact code I was using before and left it out. Also, what if the text I want is in a string? Is there an easier way to read a string line by line without having to write it to a file and read it from there?
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Parse a text file by line

    vb Code:
    1. Dim lines() As String = Str.Split(New String() {Environment.NewLine}, StringSplitOptions.None)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Parse a text file by line

    Quote Originally Posted by Vectris View Post
    I always use option strict on but I put together this example rather than the exact code I was using before and left it out.
    There shouldn't be any possibility of "leaving it out". You should have set Option Strict On in the IDE options and then it will be On by default for every project you create. You would then have to specifically set it Off at the project or file level if you wanted it Off, which you almost never would.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Parse a text file by line

    Quote Originally Posted by jmcilhinney View Post
    There shouldn't be any possibility of "leaving it out". You should have set Option Strict On in the IDE options and then it will be On by default for every project you create. You would then have to specifically set it Off at the project or file level if you wanted it Off, which you almost never would.
    I was not aware you could do that, I'll definitely change it.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width