2014-08-29 73 views
0

我得到以下代碼的錯誤:在這裏,我的pagedata.length是495,並且我在變量k中使用了其中的57個。索引超出了數組字符串的範圍

for(int k = 0; k < pagedata.Length;k++) 
     { 
      string[] textdata = pagedata.Split(new char[0]); 
      string stringforemail = textdata[k]; 
      if (stringforemail.Contains("@") && stringforemail.Contains(".")) 
      { 
       TableRow tr = new TableRow(); 
       //tr.BorderStyle = BorderStyle.Solid; 
       TableCell tc = new TableCell(); 
       tc.BorderStyle = BorderStyle.Solid; 
       tc.Text = stringforemail; 
       tr.Cells.Add(tc); 
       Table1.Rows.Add(tr); 
      } 
     } 

因爲我的代碼中有錯誤,但我無法弄清楚錯誤..請幫我理解錯誤。

謝謝

+0

什麼長度是文本數據[]? – jbutler483 2014-08-29 12:20:34

+0

顯然你的循環一直運行到'pagedata.Length',但是它比'pagedata.Split(new char [0])更高,length' - textdata [k]超出了界限。 – 2014-08-29 12:20:35

+0

我們無法真正幫助您。 Pagedata.Length> Split.NewChar.Length,因此您在尋找的內容不一致。正則表達式可能會有所幫助。 – DidIReallyWriteThat 2014-08-29 12:23:34

回答

2

您試圖在新的空字符數組上分割文本。這給你一個項目的數組,值爲pagedata

因此,您的textdata[k]將失敗,因爲它使用的長度大於1(數組長度)的pagedata

我不知道你正在嘗試用它做,但你的代碼看起來應該是這樣的:

string[] textdata = pagedata.Split("your split string"); 

foreach (string stringforemail in textdata) 
{ 
    if (stringforemail.Contains("@") && stringforemail.Contains(".")) 
    { 
     TableRow tr = new TableRow(); 
     //tr.BorderStyle = BorderStyle.Solid; 
     TableCell tc = new TableCell(); 
     tc.BorderStyle = BorderStyle.Solid; 
     tc.Text = stringforemail; 
     tr.Cells.Add(tc); 
     Table1.Rows.Add(tr); 
    } 
} 

如果你與你的分隔符使用文本替換your split string

+0

它看起來像hes將'pagedata'分解爲'0'。 因此,textdata [k],k應該是0,數組的第一個elemenet應該總是在那裏。 – DidIReallyWriteThat 2014-08-29 12:25:50

+0

@CalvinSmith:可能,但從帖子不清楚。 – 2014-08-29 12:27:19

+0

實際上,只要他在他的pagedata中傳遞了超過0個字符,textdata [k]總是會在0的位置 – DidIReallyWriteThat 2014-08-29 12:29:41

0

嘗試加入(在該月底環):

if(k==textdata.Length) 
{ 
break; 
} 

這樣一旦這已經完成,循環將打破。