1

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.

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)

aspx code:

<telerik:RadComboBox runat="server" 
                    CheckBoxes="true" 
                    ID="rcbFilterPersonType" 
                    EmptyMessage="Select Person Type" Enabled="True">
</telerik:RadComboBox>
1
  • If you just want an array of string. Why no create it right away instead of using a string builder? Commented Dec 17, 2013 at 14:49

1 Answer 1

3

To convert a StringBuilder to a string, simply call 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 StringBuilder and do something like this:

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 array of strings, there's no need for any StringBuilder or string concatenation at all. You can simply use a little Linq:

Dim strArr As String() = colGroups.Select(Function(x) x.Value).ToArray()
Sign up to request clarification or add additional context in comments.

6 Comments

It's not working my friend... I tried to do: Dim strArr() As String strArr = sbPeopleTypes.ToString() .....I'm getting that error '1-dimensional array of String' cannot be converted to 'String'
@user3002885 Are you trying to create a single string or an array of strings? Your question suggests you just want a single string.
arrays of string so I thought to convert it into a string and after that to an array of strings can you please help me?
@user3002885 Converting a list to a string, then splitting the string to get an array is like a Rube Goldberg machine--it vastly overcomplicates things. See my updated answer for a solution to get a string array .
p.s.w.g I'm getting 'Select' is not a member of 'System.Collections.Generic.IList(Of Telerik.Web.UI.RadComboBoxItem)'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.