I have a question about splitting a string and put it in a DataTable. I have a textfile like below :
blabla | blabla2 | element1 | AB:CD| blabla3
blabla | blabla2 | element2 | ABC | blabla3
blabla | blabla2 | element3 | 123 | blabla3
And the desired result is like below:
Here is my code:
using (StreamReader file = new StreamReader(files))
{
string line;
while (((line = file.ReadLine()) != null))
{
char[] delimiter = {'|'};
string pattern = @"\w*|'?[0-9a-zA-Z-:._]+'?";
Regex r = new Regex(pattern, RegexOptions.Multiline);
MatchCollection m = r.Matches(line);
foreach (Match match in m)
{
DataRow row = dt.NewRow();
string[] split = match.Groups[0].ToString().Split(delimiter);
row[split[0]] = split[1]; // gives me an exception of index
limit array
row[split[0]] = match.NextMatch().value;// gives me empty value
dt.Rows.Add(row);
}
}
}
dgVResult.DataSource = dt;
But the result from my code is not as expected, and the result is like below :