C# - 拆分数组中每个值的字符串

时间:2012-09-04 16:36:57

标签: c# .net arrays split

foreach (set.row officeJoin in officeJoinMeta)
{
    foreach (set.somethingRow confRow in myData.something.Rows)
    {
        string dep = confRow["columnName"].ToString();
        depts.Add(dep);
    }
}

我已经让这个for循环遍历一个列,将一个列中的每个值添加到dep中,然后将所有这些值存储在List<字符串>我在这个方法的顶部定义的depts。

dep中的一些值是单个字符串,如“R”,但有些值需要在逗号“R,GL,BD”之后分隔。

我理解使用.Split(“,”),但我如何拆分字符串 - 如何获取数组中的每个值,用逗号分隔它们,然后将它们存储在另一个数组中?

3 个答案:

答案 0 :(得分:2)

根据您所解释的内容撰写:

        foreach (set.row officeJoin in officeJoinMeta)
        {
            foreach (set.somethingRow confRow in myData.something.Rows)
            {
                string dep = confRow["columnName"].ToString();
                depts.AddRange(dep.Split(','));
            }
        }

答案 1 :(得分:0)

声明为

List<string[]> depts =  new List<string[]>()

并添加为

depts.Add(dep.Split(','));

答案 2 :(得分:0)

List<string> depts=new List<dept>();

    var values=dept].Split(',');

    for(int index=0;index<values.length;index++)
    {
    depts.Add(values[index].ToString());    

    }