Multi-Line TextBox => List (Of String)

jsurpless

Well-known member
Joined
Jul 29, 2008
Messages
144
Programming Experience
Beginner
Hi

I was wondering what the best way would be to convert the text in a multi-line text box to a List (Of String)?

Basically, I want to detect 'carriage returns' and translate the text btw to a list entry...

For instance,

String1
String2
String3

should =>

List(1) = String1
List(2) = String2
List(3) = String3

Thanks!
 
VB.NET:
Dim myList As New List(Of String)
  For Each line In Me.TextBox1.Text.Split(CChar(vbCrLf))
    If line.Trim.Length > 0 Then
      myList.Add(line.ToString)
    End If
  Next

Should do what your looking for. I put some logic in there that will prevent the adding of blank lines to the list that you may or may not find useful...
 
VB.NET:
StringList.AddRange(YourString.Split(vbcrlf), StringSplitOptions.RemoveEmptyEntries)
Actually forget that code use:
VB.NET:
YourStringList.AddRange(TheTextBox.Lines)
 
VB.NET:
Dim TempString As String
For x As integer = YourList.Count - 1I To 0I Step -1I
  TempString &= YourList(x)
  If x <> 0I Then TempString &= Environment.NewLine
Next x
TheTextBox.Text = TempString
If there's a lot of items (more than 5) you should use a StringBuilder instead of a String
 
VB.NET:
TheTextBox.Text = String.Join(vbNewLine, YourList.ToArray)
 
Back
Top