Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Convert Vertical List to String Array

3.00/5 (1 vote)
24 Sep 2010CPOL 16K  
This converts a vertical list to a comma-delimited list with each entry enclosed in double quotes.

With a vertical list in a textbox, just select the lines to convert and call the code from a toolstrip button. Conversion is very fast. It ignores blank lines within the list, but will tack on an extra comma if you select a blank line below the last list entry.

VB
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
  myStr = String.Empty
  Dim arr() As String = txt.SelectedText.Split(Chr(10))
  For l = 0 To arr.Length - 1
    If arr(l).Length > 0 Then
      myStr &= Chr(34) & arr(l) & Chr(34)
      If l < arr.Length - 1 AndAlso arr(l).Length > 0 Then
        myStr &= ","
      End If
    End If
  Next
  txt.SelectedText = myStr
End Sub

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)