简体   繁体   English

String []不包含ToArray()的定义,也没有扩展方法

[英]String[] does not contain the definition for ToArray() and no extension method

I am trying to store selected checkboxes values using StringBuilder and foreach loop, then convert it into the string array. 我试图使用StringBuilder和foreach循环存储选定的复选框值,然后将其转换为字符串数组。

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[] 但是我在没有将ToArray()定义为String []的这一行出现错误

string[] col = sb.ToString().Split(' ').ToArray(); 

Kindly help me how can I change my string into a string array. 请帮助我如何将我的字符串更改为字符串数组。

如您在文档中所见, Split已经返回一个字符串数组。

str.Split return an array of string. str.Split返回一个字符串数组。 No need to convert it again to Array. 无需再次将其转换为数组。

string str = sb.ToString();
string[] col = null;
int count = 0;
char[] splitchar = { ' ' };
col = str.Split(splitchar);

ToArray()方法包含在Linq中 ,因此只需使用linq即可:

using System.Linq;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM