Results 1 to 15 of 15

Thread: Split By Line

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    212

    Split By Line

    I have a text file that i want to be able to search thru for a word and then find what line it is on and only pull out that line of the text file. How can i do this?

  2. #2
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Split By Line

    Read the text file in line by line and check each line, if you find the word your looking for save the line to a variable in your code.
    If my post helps , please feel free to rate it

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    212

    Re: Split By Line

    thats the problem, i never know how big the file will be. it can be 10 lines or 200 lines

  4. #4
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Split By Line

    The number of lines doesn't matter, you don't need to know this to read in each line.

    vb Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         'variable to hold the current line
    7.         Dim strText As String
    8.         Dim strmReader As New StreamReader("C:\test.txt")
    9.         'read in each line and check it for the word
    10.         Do While strmReader.Peek > -1
    11.             strText = strmReader.ReadLine
    12.             If strText.Contains("word") Then
    13.                 'add the line of text to a listbox
    14.                 lbText.Items.Add(strText)
    15.             End If
    16.         Loop
    17.     End Sub
    18. End Class

    This reads each line until the end of the text file is reached, if it finds the 'word' it adds the line to a listbox.
    If my post helps , please feel free to rate it

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Split By Line

    Tinbeard,

    Don't forget to close the StreamReader.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  6. #6
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Split By Line

    Quote Originally Posted by stimbo
    Tinbeard,

    Don't forget to close the StreamReader.

    ahhh good point, scribbled it in a hurry but no excuse for being sloppy
    If my post helps , please feel free to rate it

  7. #7
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Split By Line

    You do not have to worry about closing the StreamReader if you enclose it in a Using block. The Using block can be used on anything that implements IDisposable.
    Prefix has no suffix, but suffix has a prefix.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    212

    Re: Split By Line

    ok, thanks, thats exactly what i needed

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    212

    Re: Split By Line

    ok, but the way the code works is, that it stops after it finds only the first one on the list. i need to be able to look for a few of them. IE: i was searching for the word "on"

    12 on 00:99
    18 on 00:88
    1 on 00:77
    19 on 00:66
    5 on 00:87

    i wanted to take the one with the highest first number and pull out the last number

  10. #10
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Split By Line

    show your code.

  11. #11
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Split By Line

    Quote Originally Posted by ethanhayon
    ok, but the way the code works is, that it stops after it finds only the first one on the list. i need to be able to look for a few of them. IE: i was searching for the word "on"

    12 on 00:99
    18 on 00:88
    1 on 00:77
    19 on 00:66
    5 on 00:87

    i wanted to take the one with the highest first number and pull out the last number
    Ahh well thats a fair bit different from the original question, what have you got so far ?

    btw the code I posted will examine every line in the text file and pull out every line with the specified search word.
    If my post helps , please feel free to rate it

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    212

    Re: Split By Line

    ok, this is what i need to do..

    1) Search text file for a string
    2) organize results by highest signal strength (number on line)
    3) test to see if the one with highest signal strength matches against a database. If not move on to next line. If it does then i need to return a value of "1"

    How can i do this?

  13. #13
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Split By Line

    1) Loop through the file by line and filter out any line that does not contain your string.
    2) Use String.Substring to get the number then cast it to an integer and compare it to the others.
    3) If it matches the use Return 1. If it doesn't match then continue the loop.

    These are general suggestions as you have not provided a code sample that you have tried. If you provide a sample then I may be able to edit it to make it work properly.
    Prefix has no suffix, but suffix has a prefix.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    212

    Re: Split By Line

    This is my code as posted above

    Code:
      Dim nwline As String
            Dim strmReader As New StreamReader("C:\Users\Ralph\Desktop\bssoutput2.txt")
            Do While strmReader.Peek > -1
                nwline = strmReader.ReadLine
                ' MsgBox(nwline)
    
                If nwline.Contains("nw") Then
                    ' MsgBox(nwline)
                    '''''' Dim strArray() = nwline.Split(" ")
    
                    ''''Dim mac As String
                    ''''mac = strArray(20)
                    'MsgBox(nwline)
                    'TextBox1.Text = (nwline)
                    '  Label1.Text. = (nwline)
                    ListBox1.Items.Add(nwline)
    
                    Dim totalnw = ListBox1.Text
                    MsgBox(totalnw)
                End If
            Loop
            strmReader.Close()
    i dont have any of the sql code in yet, but that i know how to do, the problem that i am needing help with is specifically pulling out the field with a higher signal strength and if it doesnt match, i want to be able to continue the search with the next one on the list

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    212

    Re: Split By Line

    please, does anybody know how i can do this, i am still stuck on it

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