Question Perform remove on multi-line textbox

smunning

New member
Joined
May 14, 2010
Messages
3
Programming Experience
Beginner
Hi,

Aim:
Read the text from a multi-line textbox line by line to remove all but an 8 character ID.

For example:
" Test ID: 12345678"
" Test ID: 12345678"

I wish to retrieve the ID's only and show on 1 line with a delimiter,

For example:
"12345678, 12345678"

I can do this on a single line using the following:

VB.NET:
Private Sub btnTrim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTrim.Click 

Dim sTemp As String 
Dim sCount As Integer 
sTemp = ""
 
'loads a text file into the sTemp string 
sTemp = GetFileContents(txtPath.Text)

'remove text from begining of the string 
sTemp = sTemp.Remove("0", "10") 

'check how long the sTemp string is now
sCount = sTemp - 8

'remove all text after the ID number 
sTemp = sTemp.Remove(8, sCount) 

txtData.Text = sTemp 

End Sub

I've tried to replicate this on a multi-line using Splits, Substrings and a For...Each loop - but have just ended up with a headache..

Any help would be greatly appreciated!
 
Last edited:
5 hours sleep and the 2nd attempt, got it working nicely now.

For future readers, here is my solution:

I was trying to trim just the document ID's from a string containing the following example string:

"Test ID: 12345678.tif
Test ID: 12345678.tif"

VB.NET:
Private Sub btnTrim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTrim.Click

        'declare temporary string
        Dim sTemp As String

        'loads temp data from file
        sTemp = GetFileContents(txtPath.Text)

        'define multi-lined string and split it
        Dim ReadFromString As String = sTemp
        Dim ReadFromString_LineByLine() As String = ReadFromString.Split(Environment.NewLine)

        'ensure no null values on runtime
        Dim TempLineString As String = ""
        Dim FinalString As String = ""

        'read the string line by line
        For i As Integer = 0 To ReadFromString_LineByLine.Length - 1

            'trim white spaces
            TempLineString = ReadFromString_LineByLine(i).Trim()

            'remove the first 16 chars - skip 8 (the ID) - remove the final 4
            TempLineString = ((TempLineString.Remove(0, txtBegin.Text)).Remove(txtLength.Text, 4))

            'populate the final string and enter the delimiter (default is a ",")
            FinalString = FinalString & txtDelimiter.Text & TempLineString

        Next

        'trim whitespaces before checking first/last char's
        FinalString = FinalString.Trim()

        'remove the first character if it's a delimiter
        If FinalString.StartsWith(txtDelimiter.Text) Then
            FinalString = FinalString.Remove(0, 1)
        End If

        'remove the final character if it's a delmiter
        If FinalString.EndsWith(txtDelimiter.Text) Then
            FinalString = FinalString.Remove(FinalString.Length - 1, 1)
        End If

        'final trim
        FinalString = FinalString.Trim()

        'Output the final string to a textbox
        txtData.Text = FinalString

    End Sub

Although i've predefined for 3 character extensions, i have allowed the user to choose the delimiter (usually a comma), and where the start/stop the removal of text.

Thanks
Scott
 
Regular expressions makes this a simple task. This sample code uses OpenFileDialog Class (System.Windows.Forms) to let user select the file. You can find OpenFileDialog in Toolbox.
VB.NET:
Dim input As String = IO.File.ReadAllText(fileDialog.FileName)
Dim ids As New List(Of String)
For Each m As Match In Regex.Matches(input, "\d+")
    ids.Add(m.Value)
Next
Dim out As String = String.Join(", ", ids.ToArray)
I recommend this place to learn about regular expressions: Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
 
Back
Top