Reputation: 55
I am trying to store selected checkboxes values using StringBuilder and foreach loop, then convert it into the string array.
Here is my code
public ActionResult Home(CheckList obj)
{
StringBuilder sb = new StringBuilder();
foreach (var item in obj.Checkboxes)
{
if (item.IsChecked)
sb.Append(item.Value).ToString();
}
string[] col = sb.ToString().Split(' ').ToArray();
..
}
But I got an error on this line of no definition of ToArray() to String[]
string[] col = sb.ToString().Split(' ').ToArray();
Kindly help me how can I change my string into a string array.
Upvotes: 0
Views: 2489
Reputation: 154
str.Split return an array of string. No need to convert it again to Array.
string str = sb.ToString();
string[] col = null;
int count = 0;
char[] splitchar = { ' ' };
col = str.Split(splitchar);
Upvotes: 1
Reputation: 11881
ToArray()
method is included in Linq, so simply using linq should work:
using System.Linq;
Upvotes: 3
Reputation: 16520
As you can see here in the documentation, Split
already returns a string array.
Upvotes: 4