I have below mentioned CSV string which I need to split using commas .
Input:
A,"Rakesh,Gaur",B,"A,B",Z
OutPut:
A
Rakesh,Gaur
B
A,B
Z
You can't use string split or regular expressions. If you are not going to use a library that is already built, you have to keep track of whether or not you are in_quotes. but as you will find out after you start this: csv parsing is complex. You should use something that is already pre-built. As I recall from my days writing an app that heavily relied on csv, there are escape characters and such, that you will need to account for.
Either way the psuedo code is as follows:
Stack cells = m
in_quotes = false
foreach character in string:
if character != ',' && character != '"':
cells.Top = cells.Top + character
else if character == ',' && in_quotes:
cells.Top = cells.Top + character
else if character == ',':
cells.push("")
else if character == '"' && in_quotes:
in_quotes = false
else if character == '"':
in_quotes = true
I think you can do this using following steps:
string[] words = yourStringInput.Split(',');
foreach (string word in words)
{
Console.WriteLine(word);
}