Reputation: 101
Here is my code that splits my string array using delimited characters, but does not consider the issue in my title:
char[] delimitedChars = { ',', '\n', '"' };
words = stringamabob.Split(delimitedChars);
I want this all to be true EXCEPT I don't want the comma to be a delimited character when it is inbetween quotation marks.
For example, if I had:
stringamabob = one, two, three, "four, five", six
I would get:
words [0] = one
words [1] = two
words [2] = three
words [3] = four
words [4] = five
words [5] = six
Where as I want to get:
words [0] = one
words [1] = two
words [2] = three
words [3] = four, five
words [4] = six
Upvotes: 0
Views: 826
Reputation: 67135
A regular expression like this seems to work:
"(.*)"|(\S*),|(\S*)$
As this rubular exhibits
You will end up with a match in group 1 (quotes) or group 2 (comma) or group 3 (end of line)
Upvotes: 1
Reputation: 2501
Try this, it won't work if you have quotes nested inside each other (which is rare), but it should work in all other cases.
string[] quotesplit = stringamabob.Split('"'); //Split by quotes.
char[] delimitedChars = { ',', '\n'}; //remove quotes from these delimiters because we've already split by them
List<string> words = new List<string>();
bool toggle = stringamabob.StartsWith("\""); //check if the first item is quoted
foreach(string chunk in quotesplit)
{
if(toggle) //toggle is true when we're not inside quotes
{
words.AddRange(chunk.Split(delimitedChars));
}
else
{
words.Add(chunk);
}
toggle = !toggle;
}
Upvotes: 1