How to get the whole line of text that contain a string - vb.net

There's one thing that I want to ask. How can I get the whole line of text that contain a string in Visual Basic 2010?
Let's say:
MyText.txt file contains:
Configurations:
Name: Fariz Luqman
Age: 78
My Favourite Fruit: Lemon, Apple, Banana
My IPv4 Address: 10.6.0.5
My Car: Ferrari
In Visual Basic, I want to get the whole line of text that contain the string "Banana" and print it in the textbox so it will display in that textbox:
My Favourite Fruit: Lemon, Apple, Banana
Why am I doing this? Because the text file is being appended and the line number is random. The contents is also random because the texts is generated by Visual Basic. The text "Banana" can be in line 1, line 2 or can be in any line so how can I get the whole line of text that contain certain string?
Thank you in advance!

You can do this easily all in one line with LINQ:
TextBox1.Text = File.ReadAllLines("MyText.txt").FirstOrDefault(Function(x) x.Contains("Banana"))
However, if the file is rather large, that's not particularly efficient, since it will read the whole file into memory before searching for the line. If you want to make it stop loading the file once it finds the line, could use the StreamReader, like this:
Using reader As New StreamReader("Test.txt")
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
If line.Contains("Banana") Then
TextBox1.Text = line
Exit While
End If
End While
End Using

Just checked (should have done that first!). VB.Net does have a CONTAINS() method. So:
IF line1.Contains("Banana") THEN
'do something
END IF

Related

VB.NET textbox to listbox new line

I have a list of 4 digit extensions that I'm pulling out of my server
cmd = sshClient.RunCommand("asterisk -x 'sip show peers' | grep -w OK | awk '{print $1}' | awk -F'/' '{print $1}'")
TextBox1.Text = cmd.Result
TextBox1 does fill up with the extension list, but it is all in 1 line
(btw, if I change it from TextBox1 to Label1, then I get them in separate lines)
Then, with button2 (to get the list into a ListBox) I have
Dim ext As String
ext = TextBox1.Text
ListBox1.Items.Add(ext)
But I get the list in 1 item and no spaces (it doesn't mater if it is from the TextBox or Label)
I tried adding
If TextBox1.Text.Length > 4 Then TextBox1.Text = TextBox1.Text.Insert(4, Environment.NewLine)
But it adds a new line for the first 4 digits (1 extension) only, the rest are in 1 line
Any idea?
Thanks
The string data returned by asterisk is probably separated by line feeds (ASCII value of 10). Split the return string on Chr(10) and use the .AddRange(ext) function to add all of the items to the listbox:
ListBox1.Items.AddRange(ext.Split(New Char() {Chr(10)}, StringSplitOptions.RemoveEmptyEntries))
The answer will vary a bit depending on whether you are using WinForms, WPF, etc - I am assuming WinForms.
Check that your textbox is set to Multiline = true - if not, that may fix your first problem. But more fundamentally, #LarsTech is on the right track. Assign cmd.Result to a new string variable (instead of directly to TextBox1.Text) then split it into a string array with String.Split(), then loop through the array adding each one separately to your ListBox individually. You won't be able to get multiple items into a Listbox with a single Items.Add statement regardless of how you handle your line breaks.
If that is all that your loop is doing, then you can use ListBox.AddRange() to add the whole array at once.

Copy 5th word of the "rich text box1" to "textbox1.text"

The richtextbox1 contains 8 words "The brown fox jumped over the lazy dog". I want to copy the 5th word of the richtextbox1 to textbox1.text when the button1 clicked.
can this be done using find method such as find(character, start integer, end integer) etc ie. finding "empty" (space) character and read the characters until second "empty"(space) character found. or any other better method?
I would suggest using the split function for this scenario.
e.g
textbox1.text = richtextbox1.text.split(" ")(4) 'This is for the 5th word.
You may also use the Mid function indeed. Chech it out from
https://msdn.microsoft.com/en-us/library/05e63829(v=vs.90).aspx
Updated
textbox1.text = Mid(richtexbox1.text, 1, 5)

Read every 5th line until the end of the text file

Hi could someone please give me an example of how to read through a text file to the end and read every 5th line as a string? I know how to read a specific line in the text file using
line = System.IO.file.readAllLines(filepath)(linenum)
and also using streamreader to read each line etc..
But I want to go through the whole text file and pick out every certain number of lines. Pretty sure it's got something to do with a loop but I'm not too clued up yet.
As files are not line based, you would need to read all the lines and pick out the ones that you want.
You can use the Where method with the overload that gives you the index of the item, and the Mod operator to determine where every fifth line is:
Dim lines As String() = _
System.IO.File.ReadLines(filepath).Where(Function(line, i) i Mod 5 = 0).ToArray()
The number that you compare the expression to determines which lines you get. i Mod 5 = 0 starts at the first line and then every fifth from there, while i Mod 5 = 4 starts at the fifth line and then every fifth from there.
(The ReadLines method is better than the ReadAllLines for this, as it doesn't read all the lines into memory first, but returns an enumerator so that you can process the lines as they are read.)

how to add each line richtextbox into array vb.net

I want to Split the the text in the RichTextBox per line into array in vb.net
if i show array(0) it will show the first line of richtextbox, array(1) will show the second line and go on.
Im new in VB and I cant find any decent link or site. Thanks in advance for help.
Your Richtextbox has a property called Lines (link). Which returns an String() where each line is a new entry in the Array. More info can be found in the link above.
Code example
Dim myArr As String() = MyRichTextBox.Lines
Note: Searching "Richtextbox to array" (in google) does supply you with plenty of helpful webpages

VB notepad spliter

So I just started a new program and in this program I need a feature that will basically take a text file or just a richtextbox, and then equally split it. So lets say I have a file with 200 lines of text. Then I want to split it up into 10 files of 20 lines. Please help I need some type of direction! -thanks
Count the textbox length by (textbox1.text.length)i.e.=200 character and divide it by 2 = 100 then make a loop which goes to 200 then keep condition in loop body if it equal to 100 put it to string variable and write it to file and so on and so forth...
First of all create a string array
Dim S() As String = TextBox1.Text.Split(vbNewLine)
this will allow you to put every line in an item of the array named S
to get the lines count use
S.Length
OR
S.Count()
now you got the lines and the lines' count all you have to do to divide them and go from there...