在 VB.NET 中将字符串生成器转换为字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20641898/
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
Converting String Builder to Strings Array in VB.NET
提问by user3002885
I have a RadComoboBox with check boxes and I want to loop through the check boxes with String Builder. After getting the values in that format: {1,2,3,4} I want to convert it to stringArray in that format (1,2,3,4) Can anyone please show me how I can do this?
我有一个带有复选框的 RadComoboBox,我想使用 String Builder 遍历复选框。获取该格式的值后:{1,2,3,4} 我想将其转换为该格式的 stringArray (1,2,3,4) 谁能告诉我如何做到这一点?
This is my code:
这是我的代码:
Dim sbPeopleTypes As New StringBuilder()
Dim colGroups As IList(Of RadComboBoxItem) = rcbFilterPersonType.CheckedItems
For Each item As RadComboBoxItem In colGroups
sbPeopleTypes.Append(item.Value + ",")
Next
aspx code:
aspx代码:
<telerik:RadComboBox runat="server"
CheckBoxes="true"
ID="rcbFilterPersonType"
EmptyMessage="Select Person Type" Enabled="True">
</telerik:RadComboBox>
回答by Steven Doggart
You can get the array by using the string's Splitmethod, like this:
您可以使用字符串的Split方法获取数组,如下所示:
Dim myArray() As String = sbPeopleTypes.ToString.Split(","c)
However, unless you need to use the StringBuilder, for some other reason, it would be better to use a list, like this:
但是,除非StringBuilder出于其他原因需要使用 ,否则最好使用列表,如下所示:
Dim peopleTypes As New List(Of String)()
For Each item As RadComboBoxItem In colGroups
peopleTypes.Add(item.Value)
Next
Then, if you really need it as an array, you can convert it like this:
然后,如果你真的需要它作为一个数组,你可以像这样转换它:
Dim myArray() As String = peopleTypes.ToArray()
Even if you did need it formatted as a comma delimited string, you could still build that from the list easily, without a StringBuilder, like this:
即使您确实需要将其格式化为逗号分隔的字符串,您仍然可以轻松地从列表中构建它,而无需StringBuilder,如下所示:
Dim csv As String = String.Join(",", peopleTypes)
Alternatively, since the RadComboBox.CheckedItemsproperty is an IList(Of RadComboBoxItem), you could use the SelectLINQ extension method to get the values as a list or array without the Forloop, like this:
或者,由于RadComboBox.CheckedItems属性是IList(Of RadComboBoxItem),您可以使用SelectLINQ 扩展方法将值作为列表或数组获取For,而不需要循环,如下所示:
Dim myArray() as String = rcbFilterPersonType.CheckedItems.Select(Function(x) x.Value).ToArray()

