Following on a previous thread Object reference on array with regex.replace()
I have few columns in cpath
which contain fields such as street,""test,format"", casio
. Unfortunately,
csvReader.SetDelimiters(new string[] { "," });
adds empty extra columns to the DataTable
csvData
because of the comma between test and format
. Hence the the following interrogation:
Does a trick exist to remove the comma from """test,format"""
so as to get street,""test format"", casio
in csvData
?
Thanks in advance
EDIT
private static List<string[]> RemoveComaDataFromCSVFile(string csv_file_path)
{
List<string[]> allLineFields = new List<string[]>();
try
{
using (TextReader sr = new StringReader(csv_file_path))
using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.Delimiters=new[] { "," };
csvReader.HasFieldsEnclosedInQuotes = true;
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
string pattern = "\"";
string replacement = "";
Regex rgx = new Regex(pattern);
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
//Remove quotation marks on Fields
fieldData[i] = Regex.Replace(fieldData[i], pattern, replacement);
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
//csvData.Rows.Add(fieldData);
allLineFields.Add(fieldData);
}
}
}
catch (Exception ex)
{
}
return allLineFields;
}