string[] text = File.ReadAllLines("file.csv", Encoding.Default);
foreach (string line in text)
{
string[] words = line.Split(',');
foreach (string word in words)
{
Console.WriteLine(word);
}
}
Console.ReadKey();
data = new List<string>();
parts = source.Split("\"");
data.AddRange(parts.Where((x, index) => index % 2 != 0));
data.AddRange(parts.Where((x, index) => index % 2 == 0).Split(","));
result = string.Join(" | ", data.Where(x => !string.IsNullOrWhiteSpace(x));
using System;
using System.Text;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
Console.WriteLine(string.Join("|", Parse("Мама,\"мыла, блин\", раму,\"мама, мыла \"\"раму\"\"\",конец")));
}
public enum StateEnum{Start, StartQuot, Inline, InlineQuot}
public static IEnumerable<string> Parse(string str)
{
var state = StateEnum.Start;
var sb = new StringBuilder();
foreach(var ch in str)
{
switch(ch)
{
case '"':
switch(state)
{
case StateEnum.Start:
state = StateEnum.StartQuot;
continue;
case StateEnum.StartQuot:
case StateEnum.InlineQuot:
state = StateEnum.Inline;
sb.Append('"');
continue;
case StateEnum.Inline:
state = StateEnum.InlineQuot;
continue;
}
break;
case ',':
switch(state)
{
case StateEnum.Start:
case StateEnum.InlineQuot:
yield return sb.ToString();
sb.Clear();
state = StateEnum.Start;
continue;
case StateEnum.StartQuot:
case StateEnum.Inline:
sb.Append(',');
state = StateEnum.Inline;
continue;
}
goto default;
default:
sb.Append(ch);
break;
}
}
yield return sb.ToString();
}
}