I have the data table that is read from csv file. Then it is iterated through row and columns, and each value is appended before writing to the file that is also destination csv file. I want to separate the data of one column upon the special character (“/”), into two columns. For example, the column of ‘Type’ of data table is “women/shoes and handbags/guess”. I have another column ‘SubType’, so I want to separate one column to two columns in data table before writing. I just want to ignore third type that is guess. Is there a way to seek position “/” and after second “/”, I want to insert that value into another column of data table that is ‘SubType’.
foreach (DataRow dRow in dtSor.Rows)
{
for (int i = 0; i < dtSor.Columns.Count; i++)
{
if (dRow[i].ToString().Contains(","))
{
dest_csv.Append("\"" + dRow[i].ToString() + "\"" + ",");
}
else if (dRow[i].ToString() == "")
{
dest_csv.Append("NULL" + ",");
}
else
{
dest_csv.Append(dRow[i].ToString() + ",");
//dest_csv.Append(dRow[i].ToString());
}
}
dest_csv.Remove(dest_csv.Length - 1, 1);
dest_csv.Append(Environment.NewLine);
}
File.WriteAllText(destination_file, dest_csv.ToString(), Encoding.Default);
}
First check if the special char is in the field with indexOf, then split the field on the special char in an araay and take only the parts you’re intereseted in.
like so: