Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comma Separated List of Array Items

Tags:

vb.net

Is there a built-in function in VB.NET which would take an array of strings and output a string of comma separated items?

Example: function( { "Sam","Jane","Bobby"} ) --> "Sam, Jane, Bobby"

like image 308
Steven Avatar asked Feb 11 '10 21:02

Steven


People also ask

How do you separate array elements with commas?

Answer: Use the split() Method You can use the JavaScript split() method to split a string using a specific separator such as comma ( , ), space, etc. If separator is an empty string, the string is converted to an array of characters.

How do you convert an array element to a comma-separated string?

To convert an array to a comma-separated string, call the join() method on the array, passing it a string containing a comma as a parameter. The join method returns a string containing all array elements joined by the provided separator.

How do you add a comma to an array?

Join Together an Array as a String with Commas. When you need to join together every item in a JavaScript array to form a comma-separated list of values, use . join(",") or equivalently just . join() without any arguments at all.


2 Answers

String.Join(",", YourArray) 

Additionally, if you want to get all selected items from a checkboxlist (or radiobuttonlist) you could use an extension method (checkboxlist shown below):

Call Syntax: Dim sResults As String = MyCheckBoxList.ToStringList()

    <Extension()> _
    Public Function ToStringList(ByVal cbl As System.Web.UI.WebControls.CheckBoxList) As String
        Dim separator As String = ","
        Dim values As New ArrayList
        For Each objItem As UI.WebControls.ListItem In cbl.Items
            If objItem.Selected Then
                values.Add(objItem.Value.ToString)
            End If
        Next
        Return String.Join(separator, values.ToArray(GetType(String)))
    End Function
like image 199
Kyle B. Avatar answered Oct 25 '22 19:10

Kyle B.


Use string.Join:

string commaSep = string.Join(",", myArray);
like image 20
Oded Avatar answered Oct 25 '22 18:10

Oded