Results 1 to 23 of 23

Thread: [RESOLVED] To Extract Lines with Strings in Array in MultiLine Textbox it displays only last...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2021
    Posts
    172

    Resolved [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

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Addicted Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    180

    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2021
    Posts
    172

    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

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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…

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

  9. #9
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    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

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last

    Quote Originally Posted by vbdotnut View Post
    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

  11. #11
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    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

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last

    Quote Originally Posted by vbdotnut View Post
    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

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    May 2021
    Posts
    172

    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.

  14. #14
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    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

  15. #15
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    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
    Name:  samText.jpg
Views: 232
Size:  39.2 KB

    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.

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    May 2021
    Posts
    172

    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

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: To Extract Lines with Strings in Array in MultiLine Textbox it displays only last

    Quote Originally Posted by SamDsouza View Post
    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

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    May 2021
    Posts
    172

    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

  21. #21
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    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.

  22. #22
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] To Extract Lines with Strings in Array in MultiLine Textbox it display

    Quote Originally Posted by ChrisE View Post
    there was no need to change the Code, blank;empty Lines are removed
    SamDsouza wanted to ALLOW some empty lines

  23. #23
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: [RESOLVED] To Extract Lines with Strings in Array in MultiLine Textbox it display

    Quote Originally Posted by .paul. View Post
    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
  •  



Click Here to Expand Forum to Full Width