Usually, when you have embedded commas in a CSV file, then the fields concerned are placed within quotes. For example:
dave,1,"red,blue"
bert,2,"red,green"
fred,3,"red,blue,green"
The trick then is to replace the embedded commas with some character that you know won't occur (say character 127 DEL), do the split on the remaining comma, replace the substitute characters with commas again and remove the quotes. Here's some code for doing that:
using System;
using System.IO;
class Test
{
static void Main()
{
string[] lines = File.ReadAllLines("rakesh.txt");
for(int i = 0; i < lines.Length; i++)
{
ProcessEmbeddedCommas(ref lines[i]);
string[] items = lines[i].Split(',');
for(int j = 0; j < items.Length; j++) items[j] = items[j].Replace('\x7f', ',').Replace("\"", "");
Console.WriteLine("{0} {1} {2}", items[0], items[1], items[2]);
}
Console.ReadKey();
}
static void ProcessEmbeddedCommas(ref string text)
{
bool embedded = false;
bool changed = false;
char subst = '\x7f';
char[] chars = text.ToCharArray();
for(int i = 0; i < chars.Length; i++)
{
if(chars[i] == '\"')
{
embedded = !embedded;
}
else if (chars[i] == ',' && embedded)
{
chars[i] = subst;
changed = true;
}
}
if (changed) text = new string(chars);
}
}
The output should be:
dave 1 red,blue
bert 2 red,green
fred 3 red,blue,green
If you don't use quotes or some other delimiter to mark the fields with embedded quotes, then the task is much more difficult as you don't know which commas are separators and which are embedded in the field.
In your particular example, if you can assume that there will always be five fields and that the only embedded commas will occur in the phone field which is the last one, then you could do the split in the normal way and any items above 5 must then be additional phone numbers.