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"
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.
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.
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.
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
Use string.Join
:
string commaSep = string.Join(",", myArray);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With