Skip to content

Commit 0b25625

Browse files
committed
add remove Apostrophes overload
1 parent 428fbc5 commit 0b25625

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

CSVEditorFunctions/CSVEditor.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ public void ReadFile(string FilePath)
3030
TranslateCSVtoTable();
3131
}
3232

33+
/// <summary>
34+
/// Convert the CSV file into a Datatable within the class
35+
/// </summary>
36+
/// <param name="FilePath">Path of file to load</param>
37+
/// <param name="RemoveApostrophes">inculde a " at the front and end of each field</param>
38+
public void ReadFile(string FilePath, bool RemoveApostrophes = false)
39+
{
40+
Currentfile = new CSVFile(FilePath);
41+
42+
TranslateCSVtoTable(RemoveApostrophes); // pass in boolean
43+
}
44+
3345
/// <summary>
3446
/// Write the file to the current Filepath
3547
/// </summary>
@@ -112,5 +124,43 @@ private void TranslateCSVtoTable()
112124
CSVDT.Rows.Add(dr);
113125
}
114126
}
127+
128+
/// <summary>
129+
/// Convert the CSV file into a Datatable within the class
130+
/// </summary>
131+
/// <param name="RemoveApostrophes">Removes all Apostrophes</param>
132+
private void TranslateCSVtoTable(bool RemoveApostrophes)
133+
{
134+
CSVDT = new DataTable();
135+
136+
//header
137+
138+
Regex CSVParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
139+
string[] Headers = CSVParser.Split(Currentfile.FileContents[0]);
140+
141+
foreach (string Header in Headers)
142+
{
143+
if (RemoveApostrophes == true)
144+
CSVDT.Columns.Add(Header.Replace("'", ""));
145+
else
146+
CSVDT.Columns.Add(Header);
147+
}
148+
149+
//text
150+
for (int i = 1; i < Currentfile.FileContents.Count; i++)
151+
{
152+
string[] CurrentRow;
153+
if (RemoveApostrophes == true)
154+
CurrentRow = CSVParser.Split(Currentfile.FileContents[i].Replace("'", ""));
155+
else
156+
CurrentRow = CSVParser.Split(Currentfile.FileContents[i]);
157+
158+
DataRow dr = CSVDT.NewRow();
159+
160+
dr.ItemArray = CurrentRow;
161+
162+
CSVDT.Rows.Add(dr);
163+
}
164+
}
115165
}
116166
}

0 commit comments

Comments
 (0)