使用 regex.replace() 对数组的对象引用
Object reference on array with regex.replace()
我正在使用下面的 function
将 csv
文件导入 datatable
对象 csvData
,但是我遇到了一个 oject reference
问题希望在使用 Regex.Replace
从数据中删除引号时了解更多信息:
private static DataTable Gettabledata(string cpath)
{
DataTable csvData = new DataTable();
try
{
using (TextFieldParser csvReader = new TextFieldParser(cpath))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
string pattern="\""; % remove quotation mark "
string replacement=""; % replace by empty.(eg "a", a)
Regex rgx = new Regex(pattern);
for (int i = 0; i < fieldData.Length; i++)
fieldData[i] = Regex.Replace(fieldData[i],pattern, replacement); %object reference issue
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
csvData.Rows.Add(fieldData);
}
}
}
catch (Exception ex)
{
}
return csvData;
}
fieldData[i] = Regex.Replace(fieldData[i],pattern, replacement);
在 {...}
块之外。它应该在里面。
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
else
{
fieldData[i] = Regex.Replace(fieldData[i],pattern, replacement);
}
}
我正在使用下面的 function
将 csv
文件导入 datatable
对象 csvData
,但是我遇到了一个 oject reference
问题希望在使用 Regex.Replace
从数据中删除引号时了解更多信息:
private static DataTable Gettabledata(string cpath)
{
DataTable csvData = new DataTable();
try
{
using (TextFieldParser csvReader = new TextFieldParser(cpath))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
string pattern="\""; % remove quotation mark "
string replacement=""; % replace by empty.(eg "a", a)
Regex rgx = new Regex(pattern);
for (int i = 0; i < fieldData.Length; i++)
fieldData[i] = Regex.Replace(fieldData[i],pattern, replacement); %object reference issue
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
csvData.Rows.Add(fieldData);
}
}
}
catch (Exception ex)
{
}
return csvData;
}
fieldData[i] = Regex.Replace(fieldData[i],pattern, replacement);
在 {...}
块之外。它应该在里面。
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
else
{
fieldData[i] = Regex.Replace(fieldData[i],pattern, replacement);
}
}