Click here to Skip to main content
15,883,894 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Convert Vertical List to String Array

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
24 Sep 2010CPOL 15.8K   2   1
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)


Written By
United States United States
I'm not an IT guy. Programming has been a hobby for me (and occasionally useful) ever since a sister in-law introduced me to a TI-99 4/A about a million years ago.

The creative challenge is relaxing and enjoyable. As such, I'd never mess up a fun hobby by doing it for a living.

Now, if I can just get Code Project to add "Truck Driver" to the list of job titles in the profiles...

Comments and Discussions

 
GeneralReason for my vote of 3 A useful operation, but not the most... Pin
BillW337-Oct-10 3:07
professionalBillW337-Oct-10 3:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.