If only String.Split used strings instead of chars/char
arrays to specify the delimiter/s!
One solution that springs to mind, while not pretty, is
to just iterate through your array looking for elements
that contain double quotes. Assuming your original
string was correctly formatted such that all opening
double quotes had corresponding closing quotes, it should
work (although might take a while if your CSV strings are
huge). Here's a sample of what I mean (note that I have
not parsed, compiled or tested this code fragment - typos
or syntax errors are free bonuses! Also you could
probably make this more elegant - this is just to
illustrate the concept):
string origStr = "a, b, c, \"d,e,f,g\", abcdef";
string[] newArray = origStr.Split(',');
ArrayList finalValues = new ArrayList();
bool foundFirstQuote = false, foundSecondQuote = false;
StringBuilder newElem;
for (i=0; i<newArray.Length; i++)
{
string elem = newArray
;
if ((elem.IndexOf("\"") >= 0) || foundFirstQuote)
{
// Our element looks like "a or a"
if (foundFirstQuote)
{
// We got the last value in the multival column
foundSecondQuote = true;
}
else
{
// We got the first val in the multival column
// - start building a single string of comma
// separated values until we hit the next
// double quote
newElem = new StringBuilder();
foundFirstQuote = true;
}
if (newElem.Length > 0)
{
// Separate the values with commas
newElem.Append(",");
}
// Add the new value to the comma-separated list
newElem.Append(elem);
if (foundSecondQuote)
{
// We have now processed the last value in
// the list - add the whole list to our
// ArrayList.
finalValues.Add(newElem.ToString());
foundFirstQuote = false;
foundSecondQuote = false;
}
}
else
{
// This value contained no double quotes - just
// add it as-is to the ArrayList.
finalValues.Add(elem);
}
}
Hope this helps. Sorry there was not a simpler answer!
-----Original Message-----
Happy New Year to all!
I am currently developoing an application that imports
data from a CSV file. Each comma represents an array item
that I need to extract data with.
My problem is this...
I am encountering a string that has the example below:
a, b, c. "d,e,f,g", abcdef
----The data that has double quotes is considered to be
one column.
Whenever I am using the String.Split(',') method, it
keeps on breaking the commas inside the quotes.
How can I prevent this so that it would treat the commas
within the quotes as a single unit?
I would really appreciate the help!