The Comma Separated Value (CSV) file contains data that (as the name suggests) are separated by commas. In C# they can easily be split into an array by using Data.Split(','). The problem occurs when data contains a comma. To overcome this problem, any data that contains comma is enclosed with double quotes ("").
The following function can split a csv data into array and preserve the double quoted fields. This is a sample function, you pass a single comma separated line of data. By default the separator is a comma.
I have used this in a project and seems to be working. Please do your own testing before using it and if you encounter any problem or issues let me know, so I can fix it.
private string[] csvParser(string csv, char separator = ',')
{
List <string> parsed = new <string
string[] temp = csv.Split(separator);
int counter = 0;
string data = string.Empty;
while (counter < temp.Length)
{
data = temp[counter].Trim();
if (data.Trim().StartsWith("\""))
{
bool isLast = false;
while (!isLast && counter < temp.Length)
{
data += separator.ToString() + temp[counter + 1];
counter++;
isLast = (temp[counter].Trim().EndsWith("\""));
}
}
parsed.Add(data);
counter++;
}
return parsed.ToArray();
}
Test data:
Only One Field
First, Second
"First-A, First-B", Second, Third, Fourth, Fifth
First, "Second-A, Second-B", Third, Fourth, Fifth
First, "Second-A, Second-B, Second-C", "Third-A, Third-B", Fourth, Fifth
First, Second, Third, Fourth, "Fifth-A, Fifth-B"
"First-A, First-B, First-C", "Second-A, Second-B, Second-C", Third, Fourth, "Fifth-A, Fifth-B, Fifth-C"
"First-A, First-B, First-C", "Second-A, Second-B, Second-C", "Third-A, Third-B, Third-C, Third-D", "Fourth-A, Fourth-B, Fourth-C, Fourth-D, Fourth-E", "Fifth-A, Fifth-B, Fifth-C"
The following function can split a csv data into array and preserve the double quoted fields. This is a sample function, you pass a single comma separated line of data. By default the separator is a comma.
I have used this in a project and seems to be working. Please do your own testing before using it and if you encounter any problem or issues let me know, so I can fix it.
private string[] csvParser(string csv, char separator = ',')
{
List
- >
string[] temp = csv.Split(separator);
int counter = 0;
string data = string.Empty;
while (counter < temp.Length)
{
data = temp[counter].Trim();
if (data.Trim().StartsWith("\""))
{
bool isLast = false;
while (!isLast && counter < temp.Length)
{
data += separator.ToString() + temp[counter + 1];
counter++;
isLast = (temp[counter].Trim().EndsWith("\""));
}
}
parsed.Add(data);
counter++;
}
return parsed.ToArray();
}
Test data:
Only One Field
First, Second
"First-A, First-B", Second, Third, Fourth, Fifth
First, "Second-A, Second-B", Third, Fourth, Fifth
First, "Second-A, Second-B, Second-C", "Third-A, Third-B", Fourth, Fifth
First, Second, Third, Fourth, "Fifth-A, Fifth-B"
"First-A, First-B, First-C", "Second-A, Second-B, Second-C", Third, Fourth, "Fifth-A, Fifth-B, Fifth-C"
"First-A, First-B, First-C", "Second-A, Second-B, Second-C", "Third-A, Third-B, Third-C, Third-D", "Fourth-A, Fourth-B, Fourth-C, Fourth-D, Fourth-E", "Fifth-A, Fifth-B, Fifth-C"
Doesn't handle special characters well from what I can see; keep getting IndexOutOfRange exceptions.
ReplyDeleteYes it has not been tested for special characters. Let us know if you have a resolution.
ReplyDelete