2012-09-04 69 views
1
foreach (set.row officeJoin in officeJoinMeta) 
{ 
    foreach (set.somethingRow confRow in myData.something.Rows) 
    { 
     string dep = confRow["columnName"].ToString(); 
     depts.Add(dep); 
    } 
} 

我有這個for循環通過一列去,一列中添加的每個值DEP,後來存儲所有這些在列表<字符串>科指南我在這個方法的頂部定義了它。C# - 數組中的每個值的分割字符串

dep中的一些值是單個字符串,如「R」,但有些需要在逗號「R,GL,BD」之後分隔。我如何分割字符串 - 我如何獲得數組中的每個值,用逗號分割它們,然後將它們存儲在另一個數組中?

+0

你想有一個數組的列表,還是你想合併結果成一個單一的序列? –

+1

我假設這些foreach語句之間有更多的代碼,否則他們沒有多大意義,爲什麼你會在另一個似乎沒有關聯的foreach中做foreach。 – McAden

回答

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

有條件似乎是多餘的。只需採取AddRange分支。如果未找到拆分字符,Split將返回包含整個字符串值的數組。 – vossad01

+0

@ vossad01你是對的。 – McAden

0

聲明爲

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

,並添加爲

depts.Add(dep.Split(',')); 
0
List<string> depts=new List<dept>(); 

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

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

    }