Question Help with Splitting a multi-line textbox into an array

mwill900

New member
Joined
Jul 25, 2008
Messages
2
Programming Experience
Beginner
Hello Everyone,

I have a multi-line text box that people will will cut and paste numbers from an excel file from. Once the numbers are pasted into the text box I split the lines like this (found it on the internet somewhere):

VB.NET:
Dim strArr() As String = Me.txtInputSerials.Text.Split(ChrW(10))

I then want to wrap single quotes around each number like this: '1' the code I am using for that is:

VB.NET:
For i As Integer = 0 To Me.txtInputSerials.Text.Split(ChrW(10)).Length - 1
       MsgBox("'" & strArr(i) & "'")
Next

However, when I run the code the following happens:

First number looks correct: '1'
The rest of the numbers look like there is a return character in front of the number so it looks like this:

'
2'

How do I split the multi-line textbox and get the values into an array that has an output of: '1' '2' '3' etc...?

I am new to VB.NET and have been searching for an answer. Any help would be appreciated.

Thanks. Mike
 
The problem is that you're splitting on the carriage return and leaving a line feed on the array elements. You can handle that by using the replace member function and putting an empty string in its place.

VB.NET:
        Dim strArr() As String = Me.txtInputSerials.Text.Replace(ControlChars.Lf, String.Empty).Split(ControlChars.NewLine)

        For i As Integer = 0 To strArr.Length - 1
            MsgBox("'" & strArr(i) & "'")
        Next

I would also use the length of the array - 1 in your FOR loop as you've already done the split when dimensioning your array.
 
You could split the Text property using the Environment.NewLine character

or even easier you can use Textbox's Lines() property:
VB.NET:
For Each strLine As String In TextBox1.Lines
  'strLine is the current line
Next strLine
Then all you need to do is add the quotes and store them:
VB.NET:
Dim Items As New List(Of String)
For Each strLine As String In TextBox1.Lines
  Items.Add("'" & strLine & "'")
Next strLine
 
JuggaloBrotha / MattP,

Thank you very much for your help and for the quick replies. Now I am able to continue on with the program.

Thanks again!

Mike
 
Back
Top