简体   繁体   中英

Index was outside the bounds of the array string

I am getting an error for following code: Here my pagedata.length is 495 and i am using 57 of these in variable k.

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);
            }
        }

of cause there is something wrong in my code, but i cant figure out the mistake.. please help me understanding error.

Thank you

You are trying to split your text on a new, empty character array. That gives you an array with one item, the value of pagedata .

Hence, your textdata[k] will fail since that uses the length of pagedata , which is more than 1 (the length of the array).

I don't know what you are trying to do with it, but your code should look something like this:

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);
    }
}

Where you replace your split string with the text you use as delimeter.

try adding (at the end of the for loop):

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

that way once this has been completed, the loop will break.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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