vb.net 逗号分隔的数组项列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2247875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:41:10  来源:igfitidea点击:

Comma Separated List of Array Items

vb.net

提问by Steven

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

VB.NET 中是否有一个内置函数,它可以接受一个字符串数组并输出一串逗号分隔的项目?

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

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

回答by Kyle B.

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()

调用语法: 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

回答by Oded

Use string.Join:

使用string.Join

string commaSep = string.Join(",", myArray);

回答by froadie

Use

String.Join(",", arrayWithValues)

See here

这里

回答by Program.X

I don't know about VB, but C# has a String.Join method which can concatanate a string array delimited by a nominated character. Presume VB is almost identical.

我不了解 VB,但 C# 有一个 String.Join 方法,它可以连接由指定字符分隔的字符串数组。假设 VB 几乎相同。