Good Morning,
I have to run the split of a string and this generate a Datatable.
The string in my possession is of the type:
'B1M3T2' or it could be B10M3T22 '
where the order is not always the same, and you have to read them in pairs:
B1 - M2 - T2
After the split should generate a DataTabele:
CHAR INT
B 1
M 2
T 2
For the split I was using as a separator char array, but I have control over the order ...
Suggestions?
Thank You.
Loading

Vikram AgrawalPosted Mar 17, 2015, 5:36 AM
private static DataTable GenerateDataTable(string formattedString)
{
DataTable myDT = new DataTable();
myDT.Columns.Add("Char");
myDT.Columns.Add("Int");
string strChar="", strInt="";
for (int i = 0; i < formattedString.Length; i++)
{
if (!Char.IsDigit(formattedString[i]))
{
strInt = "";
strChar = formattedString[i].ToString();
}
else
{
strInt = strInt + formattedString[i];
}
if((i!= formattedString.Length -1))
{
if(!Char.IsDigit(formattedString[i+1]) )
{
DataRow myDR = myDT.NewRow();
myDR["Char"] = strChar;
myDR["Int"] = strInt;
myDT.Rows.Add(myDR);
}
}
}
return myDT;
}
thanks
VulpesPosted Mar 17, 2015, 8:23 AM
The output is:
Roberto SalemiPosted Mar 17, 2015, 5:00 AM
Vikram AgrawalPosted Mar 17, 2015, 4:57 AM
Hi Roberto,
Do you want your output like this :
for 'B1M3T2'
Char Int
B 1
M 3
T 2
for B10M3T22
B 10
M 3
T 22
if not then please give some more scenarios
Thanks