-
Feb 7th, 2023, 09:25 AM
#1
Thread Starter
Lively Member
[RESOLVED] To Extract Lines with Strings in Array in MultiLine Textbox it displays only last...
Hello
My txtbx3 contains the following Data.
Credit Card Style
Reference No.: BV09024154692320
From Account.: 71631958856231
Card No.: 6357262XXXXXX975
Card Type: XXXX CARD
Last Statement Bal: 32,069.00
I would Like to extract Lines with Strings in Array in MultiLine Textbox
I tried the following code but displays only Last Line of txtbox3 into txtbx4 ie "Last Statement Bal: 32,069.00"
Code:
Public Class Xtract_String_From_Multiline_Frm
Public arrayStringsTxt(0 To 4) As String
Public sLines() As String
Public strmatch As String
Public Eachlinetxt As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim j As Long
Eachlinetxt = txtBx3.Text
sLines = Eachlinetxt.Split({vbCrLf, vbCr, vbLf}, StringSplitOptions.None)
arrayStringsTxt(0) = "Reference No.:"
arrayStringsTxt(1) = "From Account.:"
arrayStringsTxt(2) = "Card No.:"
arrayStringsTxt(3) = "Card Type:"
arrayStringsTxt(4) = "Last Statement Bal:"
For j = LBound(arrayStringsTxt) To UBound(arrayStringsTxt)
strmatch = sLines.FirstOrDefault(Function(x) x.Contains(arrayStringsTxt(j)))
j = j + 1
Next j
txtBx4.Text = txtBx4.Text & strmatch
End Sub
Therfore txtbx4 to display as below
Reference No.: BV09024154692320
From Account.: 71631958856231
Card No.: 6357262XXXXXX975
Card Type: XXXX CARD
Last Statement Bal: 32,069.00
Thaknx
SamD
93
-
Feb 7th, 2023, 09:34 AM
#2
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
Not sure if I missed something but this produces the correct results,
Code:
Dim lns As List(Of String) = txtb3.Lines.ToList 'get the lines
lns.RemoveAt(0) 'remove the first one
txtb4.Lines = lns.ToArray 'the result
-
Feb 7th, 2023, 09:34 AM
#3
Addicted Member
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
Well it will do as your appending to txtBx4.text after the loop has finished using the last value in your array
-
Feb 7th, 2023, 09:48 AM
#4
Thread Starter
Lively Member
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
dbasnett
Although the structure shows to remove the First Line its OK But what if there are different Lines in between with Non Array Strings defined.
I would only like to display the lines with Strings mentioned in array.
Not sure if I missed something but this produces the correct results,
Code:
Code:
Dim lns As List(Of String) = txtb3.Lines.ToList 'get the lines
lns.RemoveAt(0) 'remove the first one
txtb4.Lines = lns.ToArray 'the result
Samd
94
-
Feb 7th, 2023, 12:00 PM
#5
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
Does this look better?
Code:
Public keepThese() As String = {"Reference No.:", "Card No.:", "From Account.:"}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lns As List(Of String) = txtb3.Lines.ToList 'get the lines
For x As Integer = lns.Count - 1 To 0 Step -1 'because of remove iterate backwards
Dim fnd As Boolean = False
For a As Integer = 0 To keepThese.Length - 1
If lns(x).StartsWith(keepThese(a)) Then
fnd = True
Exit For
End If
Next
If Not fnd Then
lns.RemoveAt(x) 'not found, remove it
End If
Next
txtb4.Lines = lns.ToArray 'the result
End Sub
-
Feb 7th, 2023, 12:55 PM
#6
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
This loop is a bit of a mess:
Code:
For j = LBound(arrayStringsTxt) To UBound(arrayStringsTxt)
strmatch = sLines.FirstOrDefault(Function(x) x.Contains(arrayStringsTxt(j)))
j = j + 1
Next j
That may no longer be relevant, after dbasnett's answer, but in case that answer doesn't work, there are some oddities to that loop. For one thing, LBound and UBound are VB6 constructrs. .NET arrays all start at 0, and your array explicitly started at 0, so there's really no point in the LBound, and little point in the UBound.
You also increment J inside the loop. That will mean that you are working with every other item in the array. If you really wanted to do that, you could have used Step 2 a bit more easily.
You then just set strmatch each time, as bmwpete pointed out, which means that all strmatch holds, after you are done with the loop, is whatever was last in it. I'm thinking the rest of that line is wrong, too, but I'll leave it there, as DB seems to have a better solution that gets rid of all that.
My usual boring signature: Nothing
 
-
Feb 7th, 2023, 01:00 PM
#7
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
What is the text that you want to extract from? Do all of the lines you want to extract contain a : whereas the others don’t? You can do that in 2 lines of code…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 7th, 2023, 01:22 PM
#8
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
Ok, 4 lines of code… 
Code:
Dim mustContain() As String = {"Reference No.:", "From Account.:", "Card No.:", "Card Type:", "Last Statement Bal:"}
Dim lines As List(Of String) = txtBx3.Lines.ToList
lines.RemoveAll(Function(l) Not mustContain.Any(Function(s) l.Contains(s)))
txtBx4.Lines = lines.ToArray
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 7th, 2023, 02:37 PM
#9
Addicted Member
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
Looks like only needs the last 5 elements so maybe just get the lines From TB1 into an array and resize the array and put into TB2.
Code:
Dim TBox1Arry As String() = TextBox1.Lines.Reverse.ToArray
Array.Resize(TBox1Arry, 5)
TextBox2.Lines = TBox1Arry.Reverse.ToArray
-
Feb 7th, 2023, 03:13 PM
#10
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
 Originally Posted by vbdotnut
Looks like only needs the last 5 elements so maybe just get the lines From TB1 into an array and resize the array and put into TB2.
Code:
Dim TBox1Arry As String() = TextBox1.Lines.Reverse.ToArray
Array.Resize(TBox1Arry, 5)
TextBox2.Lines = TBox1Arry.Reverse.ToArray
That’s only in that particular example case. There could be other lines to remove too. Read post #4
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 7th, 2023, 03:50 PM
#11
Addicted Member
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
That’s only in that particular example case. There could be other lines to remove too. Read post #4
I missed the bit about string in between. That just makes me wonder what is writing this to the textbox in the first place and why even involve the control at all. Is the user inputting this string or is it an output of a routine
-
Feb 7th, 2023, 04:13 PM
#12
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
 Originally Posted by vbdotnut
I missed the bit about string in between. That just makes me wonder what is writing this to the textbox in the first place and why even involve the control at all. Is the user inputting this string or is it an output of a routine
Good question
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 8th, 2023, 07:33 AM
#13
Thread Starter
Lively Member
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
dbasnett and .paul
Thanks very much for your great help. Both the codes worked perfectly.
Although as per #4 Where i mentioned
But what if there are different Lines in between with Non Array Strings defined.
I would only like to display the lines with Strings mentioned in array.
Unfortunately, the line with Strings mentioned in array, between came a numeric value an unexpected situation in a textbox although was very clear of txtBx3 and txtbx4 structure as per #4 and #1.
I was Wondering how could i display the numeric value in between the defined String arrays which was to be displayed and these numeric Value basically is Amount or Some kind of ID where i dont have control to define in Array
I tried the following
Code:
Dim contents As String = txtBx3.Text
Dim digits As New String(contents.Where(Function(c) Char.IsDigit(c)).ToArray())
MessageBox.Show(digits)
It displayed all the Numeric Values in one line. MSG box was to see the result
ie 09024154692320716319588562316357262975234567903206900
txtbx3.Text as below
Credit Card Style
Reference No.: BV09024154692320
From Account.: 71631958856231
Card No.: 6357262XXXXXX975
Card Type: XXXX CARD
23456790
Last Statement Bal: 32,069.00
txtbx4.Text as below
Reference No.: BV09024154692320
From Account.: 71631958856231
Card No.: 6357262XXXXXX975
Card Type: XXXX CARD
23456790
Last Statement Bal: 32,069.00
Any chances to display as above quoted structure
SamD
95
Last edited by SamDsouza; Feb 8th, 2023 at 07:44 AM.
-
Feb 8th, 2023, 09:11 AM
#14
Addicted Member
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
Sounds to me instead of testing conditiond you want in to test the conditions your want out.
Code:
For Each Ln As String In TextBox1.Lines
If Not Ln.ToLower.Contains("credit card style") AndAlso Not Ln = String.Empty Then
TextBox2.Text &= Ln & Environment.NewLine
End If
Next
-
Feb 8th, 2023, 10:04 AM
#15
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
where does the Data come from ?
my take with Regex.. might be an overkill with Regex
Code:
Public Class Form4
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
CleanLines()
End Sub
Private Sub Form4_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = System.IO.File.ReadAllText("D:\TestFolder\SamText.txt")
End Sub
Private Sub CleanLines()
Dim lines As New List(Of String)
lines = TextBox1.Lines.ToList
Dim sPattern As String = "Reference No|From Account|Card No|Card Type|Last Statement Bal|(\d+)"
For i As Integer = lines.Count - 1 To 0 Step -1
If Not Regex.IsMatch(lines(i), sPattern) Then
lines.RemoveAt(i)
End If
Next
TextBox2.Lines = lines.ToArray
End Sub
End Class

EDIT:
if that Number has atleast 8 digits change the sPattern to ..
Code:
Dim sPattern As String = "Reference No|From Account|Card No|Card Type|Last Statement Bal|[0-9]{8}"
Last edited by ChrisE; Feb 8th, 2023 at 10:28 AM.
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Feb 8th, 2023, 10:10 AM
#16
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
Try this...
Code:
Dim mustContain() As String = {"Reference No.:", "From Account.:", "Card No.:", "Card Type:", "Last Statement Bal:"}
Dim lines As List(Of String) = txtbx3.Lines.ToList
lines.RemoveAll(Function(l) Not (mustContain.Any(Function(s) l.Contains(s)) Or IsNumeric(l)))
txtbx4.Lines = lines.ToArray
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 10th, 2023, 12:37 AM
#17
Thread Starter
Lively Member
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
.Paul
Indeed Fantastic the 4 line coding am really impressed.
Hope this should be my last question on this thread ie How Can i RETAIN A blank line(s) if the blank Line(s) appears between the defined Strings Arrays.
Vbdotnut and ChrisE
Thanks so much for your valuable time to post your solutions.
SamD
96
-
Feb 10th, 2023, 05:52 AM
#18
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
 Originally Posted by SamDsouza
How Can i RETAIN A blank line(s) if the blank Line(s) appears between the defined Strings Arrays.
That would be easier to implement with ChrisE’s solution
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 11th, 2023, 12:50 PM
#19
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
Code:
Public Class Form4
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
CleanLines()
End Sub
Private Sub Form4_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = System.IO.File.ReadAllText("D:\TestFolder\SamText.txt")
End Sub
Private Sub CleanLines()
Dim lines As New List(Of String)
lines = TextBox1.Lines.ToList
Dim sPattern As String = "Reference No|From Account|Card No|Card Type|Last Statement Bal|(\d+)"
For i As Integer = lines.Count - 1 To 0 Step -1
If i <> lines.Count - 1 And i <> 0 Then
If lines(i).Trim = “” Then Continue For
End If
If Not Regex.IsMatch(lines(i), sPattern) Then
lines.RemoveAt(i)
End If
Next
TextBox2.Lines = lines.ToArray
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 13th, 2023, 08:07 AM
#20
Thread Starter
Lively Member
Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last
.Paul
Thank you so much for valuable inputs. With your post i learnt something new ie If ..... Then Continue For.
With this you modified ChrisE's solution. Indeed truly thankful to you.
Code:
Public Class Form4
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
CleanLines()
End Sub
Private Sub Form4_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = System.IO.File.ReadAllText("D:\TestFolder\SamText.txt")
End Sub
Private Sub CleanLines()
Dim lines As New List(Of String)
lines = TextBox1.Lines.ToList
Dim sPattern As String = "Reference No|From Account|Card No|Card Type|Last Statement Bal|(\d+)"
For i As Integer = lines.Count - 1 To 0 Step -1
If i <> lines.Count - 1 And i <> 0 Then
If lines(i).Trim = “” Then Continue For
End If
If Not Regex.IsMatch(lines(i), sPattern) Then
lines.RemoveAt(i)
End If
Next
TextBox2.Lines = lines.ToArray
End Sub
End Class
I thank other members who have also posted their solutions on this thread.
Thank you once more.
SamD
97
-
Feb 13th, 2023, 10:50 AM
#21
Re: [RESOLVED] To Extract Lines with Strings in Array in MultiLine Textbox it display
there was no need to change the Code, blank;empty Lines are removed
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Feb 13th, 2023, 06:24 PM
#22
Re: [RESOLVED] To Extract Lines with Strings in Array in MultiLine Textbox it display
 Originally Posted by ChrisE
there was no need to change the Code, blank;empty Lines are removed
SamDsouza wanted to ALLOW some empty lines
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 14th, 2023, 02:55 AM
#23
Re: [RESOLVED] To Extract Lines with Strings in Array in MultiLine Textbox it display
 Originally Posted by .paul.
SamDsouza wanted to ALLOW some empty lines
oh got it, thanks .paul
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
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
|