在 VB.NET 中将 StringBuilder 转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20626011/
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 StringBuilder to String 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 this result into a string.
我有一个带有复选框的 RadComoboBox,我想使用 String Builder 遍历复选框。获取该格式的值后:{1,2,3,4}我想将此结果转换为字符串。
Can anyone please show me how I can do this?
谁能告诉我如何做到这一点?
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
How can I convert StringBuilder to a string and split it to string like that (1,2,3,4)
如何将 StringBuilder 转换为字符串并将其拆分为这样的字符串 (1,2,3,4)
aspx code:
aspx代码:
<telerik:RadComboBox runat="server"
CheckBoxes="true"
ID="rcbFilterPersonType"
EmptyMessage="Select Person Type" Enabled="True">
</telerik:RadComboBox>
回答by p.s.w.g
To convert a StringBuilderto a string, simply call ToString:
要将 a 转换StringBuilder为字符串,只需调用ToString:
Dim str As String = sbPeopleTypes.ToString()
But you will probably want to add "{"and "}"around the value as well.
但您可能还想添加"{"和"}"围绕该值。
Alternatively, you can skip the StringBuilderand do something like this:
或者,您可以跳过StringBuilder并执行以下操作:
Dim colGroups As IList(Of RadComboBoxItem) = rcbFilterPersonType.CheckedItems
Dim str As String = "{" + String.Join(",", colGroups.Select(Function(x) x.Value)) + "}"
Now, if what you really wanted was an arrayof strings, there's no need for any StringBuilderor string concatenation at all. You can simply use a little Linq:
现在,如果您真正想要的是一个字符串数组,则根本不需要 anyStringBuilder或 string 连接。你可以简单地使用一点 Linq:
Dim strArr As String() = colGroups.Select(Function(x) x.Value).ToArray()

