I have a string with two different delimiters (| and ~). I want to first split based on one delimiter (|) then loop through the results and split those results by the second delimiter (~). From there I want to take the three values form the second split and assign them to labels on my page.
The problem I am having is with the code below I can only get item[0], for item[1] and item[2] I get an index out of bounds error.
I am not sure where I have gone wrong here and any assistance would be greatly appreciated!
int rowNumber = 1;
foreach (string itemArray in ItemList.Text.Split('|'))
{
Label tbCM = (Label)FindControl("CM" + rowNumber);
Label tbCode = (Label)FindControl("Code" + rowNumber);
Label tbAmt = (Label)FindControl("Amt" + rowNumber);
HtmlTableRow trItem = (HtmlTableRow)FindControl("trRow" + rowNumber);
string[] item = itemArray.Split('~');
tbCM.Text = item[0].TrimStart(',');
tbCode.Text = item[1];
tbAmt.Text = item[2];
trItem.Style.Add("display", "block");
rowNumber = rowNumber + 1;
}
You must check for bad input:
BTW: You can also do multiple splits in one call: