|
-
Jun 10th, 2007, 08:00 AM
#1
Thread Starter
Addicted Member
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?
-
Jun 10th, 2007, 08:28 AM
#2
Hyperactive Member
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 
-
Jun 10th, 2007, 08:31 AM
#3
Thread Starter
Addicted Member
Re: Split By Line
thats the problem, i never know how big the file will be. it can be 10 lines or 200 lines
-
Jun 10th, 2007, 08:39 AM
#4
Hyperactive Member
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:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'variable to hold the current line
Dim strText As String
Dim strmReader As New StreamReader("C:\test.txt")
'read in each line and check it for the word
Do While strmReader.Peek > -1
strText = strmReader.ReadLine
If strText.Contains("word") Then
'add the line of text to a listbox
lbText.Items.Add(strText)
End If
Loop
End Sub
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 
-
Jun 10th, 2007, 08:56 AM
#5
Re: Split By Line
Tinbeard,
Don't forget to close the StreamReader.
-
Jun 10th, 2007, 09:33 AM
#6
Hyperactive Member
Re: Split By Line
 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 
-
Jun 10th, 2007, 12:04 PM
#7
Hyperactive Member
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.
-
Jun 10th, 2007, 12:18 PM
#8
Thread Starter
Addicted Member
Re: Split By Line
ok, thanks, thats exactly what i needed
-
Jun 10th, 2007, 04:06 PM
#9
Thread Starter
Addicted Member
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
-
Jun 10th, 2007, 04:12 PM
#10
-
Jun 11th, 2007, 02:07 PM
#11
Hyperactive Member
Re: Split By Line
 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 
-
Jun 11th, 2007, 08:15 PM
#12
Thread Starter
Addicted Member
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?
-
Jun 11th, 2007, 08:30 PM
#13
Hyperactive Member
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.
-
Jun 11th, 2007, 08:53 PM
#14
Thread Starter
Addicted Member
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
-
Jun 21st, 2007, 04:57 PM
#15
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|