Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Find string in textbox and move that line & 5 lines below to other textbox

Just like the title says,
I'm working on a program that filters out certain keywords from a textbox, i got that working without any problems but my next step is that it not only moves the keyword to another textbox but also the 5 next coming lines beneath it.

The keyword can appear more than 1 time in the textbox so it must be looped till the whole textbox is read.
Does anyone knows how i can do this?

EXAMPLE:

Textbox contains:

PHP Code

<?php 
Name
Lastname
Phone
Address
ZipCode
Shit
sdsf
gerg
ereh
ergrtt
htr
45yge
Name
Lastname
Phone
Address
ZipCode 

So what i want to filter is only this:

Name
Lastname
Phone
Address
ZipCode
Code
do untill (keyword is gone)
loop

Well you can post what you have coded and it is possible to help you from there.
(Apr 26, 2014 03:28 PM)Moen Wrote:  
Code
do untill (keyword is gone)
loop

Well you can post what you have coded and it is possible to help you from there.

this is what i got to find it:

PHP Code

<?php 
For Each line As String In File.ReadAllLines(location)
      If 
line.StartsWith("Log In") Then
            RichTextBox2
.Text = line.ToString

      End 
If
      
Next 
Are you trying to find the lines from the TextBox then or from the file directly? Using the file would be better. Find the line that you want to start at, then increment a count for how many lines you want to retrieve. When the count extends the limit, stop collecting the lines, pretty simple. :)

LINQ provides useful methods for this:
Code
File.ReadAllLines("path").SkipWhile(Function(ln) ln.IndexOf("searchvalue") == -1).Take(5)
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Apr 26, 2014 04:30 PM)0xDEAD10CC Wrote:  Are you trying to find the lines from the TextBox then or from the file directly? Using the file would be better. Find the line that you want to start at, then increment a count for how many lines you want to retrieve. When the count extends the limit, stop collecting the lines, pretty simple. :)

LINQ provides useful methods for this:
Code
File.ReadAllLines("path").SkipWhile(Function(ln) ln.IndexOf("searchvalue") == -1).Take(5)

File.ReadAllLines(location).SkipWhile(Function(ln) ln.IndexOf("Log In") = -1).Take(5)
your code wasnt working so i removed the double = i think that was a prob but now i dont really know how to execute the command to make it popup in the richtextbox :/
I have just made everything on an other way.
This will change the textbox 2 times since it is contains "Lastname" two times

Code
Dim FileReader As New IO.StreamReader("C:\Users\Moen\Desktop\Ny mappe\text.txt")
      Dim CurrentLine As String
      Dim Outputstring As String

      Do Until FileReader.EndOfStream
      CurrentLine = FileReader.ReadLine()

      If CurrentLine = "Lastname" Then
            Outputstring = CurrentLine
            For i = 1 To 5 ' runs five times
            If FileReader.EndOfStream Then 'Checs if it is at the end of the file
            i = 5
            Else
            CurrentLine = FileReader.ReadLine()
            Outputstring &= vbNewLine & CurrentLine
            End If
            Next

            TextBox1.Text = Outputstring
      End If
      Loop
(Apr 26, 2014 04:47 PM)Moen Wrote:  I have just made everything on an other way.
This will change the textbox 2 times since it is contains "Lastname" two times

Code
Dim FileReader As New IO.StreamReader("C:\Users\Moen\Desktop\Ny mappe\text.txt")
      Dim CurrentLine As String
      Dim Outputstring As String

      Do Until FileReader.EndOfStream
      CurrentLine = FileReader.ReadLine()

      If CurrentLine = "Lastname" Then
      Outputstring = CurrentLine
      For i = 1 To 5 ' runs five times
      If FileReader.EndOfStream Then 'Checs if it is at the end of the file
      i = 5
      Else
      CurrentLine = FileReader.ReadLine()
      Outputstring &= vbNewLine & CurrentLine
      End If
      Next

      TextBox1.Text = Outputstring
      End If
      Loop

Thanks thats working good but got 1 prob and that is if the currentline is like this "Lastname abc" it wont capture it is there a way to make it find "Lastname" no matter what is standing behind it? I also noticed that if i got 2 times lastname in it it wil only get the last one ;/
(Apr 26, 2014 04:53 PM)ShadyShadow Wrote:  Thanks thats working good but got 1 prob and that is if the currentline is like this "Lastname abc" it wont capture it is there a way to make it find "Lastname" no matter what is standing behind it?

then you use "startswith"
as you have used before.


(Apr 26, 2014 04:53 PM)ShadyShadow Wrote:  Thanks thats working good but got 1 prob and that is if the currentline is like this "Lastname abc" it wont capture it is there a way to make it find "Lastname" no matter what is standing behind it?

then you use "startswith"
as you have used before.
Got it working thanks, my code is:

Code
location = TextBox1.Text + "\" + ListBox1.SelectedItem.ToString
      Dim FileReader As New IO.StreamReader(location)
      Dim CurrentLine As String
      Dim Outputstring As String

      Do Until FileReader.EndOfStream
      CurrentLine = FileReader.ReadLine()

      If CurrentLine.StartsWith("Log In") Then
      Outputstring = CurrentLine
      For i = 1 To 5 ' runs five times
      If FileReader.EndOfStream Then 'Checs if it is at the end of the file
      i = 5
      Else
      CurrentLine = FileReader.ReadLine()
      Outputstring &= vbNewLine & CurrentLine
      End If
      Next

      RichTextBox2.Text = RichTextBox2.Text + Outputstring + vbNewLine + vbNewLine

      End If
      Loop
(Apr 26, 2014 04:41 PM)ShadyShadow Wrote:  File.ReadAllLines(location).SkipWhile(Function(ln) ln.IndexOf("Log In") = -1).Take(5)
your code wasnt working so i removed the double = i think that was a prob but now i dont really know how to execute the command to make it popup in the richtextbox :/

Ahh, translated it in notepad from a test in C# that I did quickly (already had a C# project open), forgot to change that. Other than that, the only other thing I had to do was change the lambda and remove the semicolon. Thanks :)

For a textbox you have to translate that IEnumerable result into a string.

Code
var obj = string.Join(Environment.NewLine, File.ReadAllLines("path").SkipWhile(ln => ln.IndexOf("searchvalue") == -1).Take(5).ToArray());

Code
Dim obj As String = string.Join(Environment.NewLine, File.ReadAllLines("path").SkipWhile(function(ln) ln.IndexOf("searchvalue") = -1).Take(5).ToArray())
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by MaskinMay 25, 2022 05:29 PM 4 374 May 25, 2022 05:29 PM
Last Post: Maskin
[C] by SIM-2362662Apr 23, 2020 07:06 PM 11 366 Apr 23, 2020 07:06 PM
Last Post: dracx
[.NET] by CendrixMar 21, 2020 09:24 AM 8 329 Mar 21, 2020 09:24 AM
Last Post: Cendrix