public static string ToCSV(string[] textlines, char quoteChar, char delimiter) { StringBuilder stringBuilder = new StringBuilder(); foreach (string textline in textlines) { stringBuilder.Append(CsvOperations.QuoteStr(textline, quoteChar)); stringBuilder.Append(delimiter); } return(stringBuilder.Remove(stringBuilder.Length - 1, 1).ToString()); }
private static IEnumerable <string> ParseQuotedStrings(string toParse, char quoteChar, char delimiter, int line) { string parsedStr = string.Empty; bool insideQuote = false; for (int i = 0; i < toParse.Length; i++) { if (toParse[i] != quoteChar & toParse[i] != delimiter) { parsedStr += toParse[i]; } else if (toParse[i] == delimiter) { if (!insideQuote) { yield return(CsvOperations.ExtractQuotedStr(parsedStr, quoteChar)); parsedStr = string.Empty; } else { parsedStr += toParse[i]; } } else if (toParse[i] == quoteChar && i != 0 && i - 1 > 0 && toParse[i - 1] == delimiter) { insideQuote = true; parsedStr += toParse[i]; } else if (toParse[i] == quoteChar & i == toParse.Length - 1) { parsedStr += toParse[i]; } else if (toParse[i] == quoteChar & toParse[i + 1] == delimiter) { insideQuote = false; parsedStr += toParse[i]; } else if (toParse[i] == quoteChar) { parsedStr += toParse[i]; } else if (toParse[i] == delimiter) { parsedStr += toParse[i]; } } yield return(CsvOperations.ExtractQuotedStr(parsedStr, quoteChar)); parsedStr = string.Empty; yield break; }
public static IEnumerable <string[]> Read( IFileSystem fileSystem, string filename, char quoteChar, char delimiter, int firstColumn, int maxColumns) { using (Stream stream = fileSystem.CreateStream(filename)) { using (StreamReader csvStream = new StreamReader(stream)) return(CsvOperations.Read(csvStream, quoteChar, delimiter, firstColumn, maxColumns)); } }
public static IEnumerable <string[]> Read( string filename, char quoteChar, char delimiter, int firstColumn, int maxColumns) { using (StreamReader reader = new StreamReader(filename)) { foreach (string[] strArray in CsvOperations.Read(reader, quoteChar, delimiter, firstColumn, maxColumns)) { yield return(strArray); } } }
public static IEnumerable <string[]> Read( StreamReader csvStream, char quoteChar, char delimiter, int firstColumn, int maxColumns) { string empty = string.Empty; int line = 0; List <string[]> strArrayList = new List <string[]>(); string toParse; while ((toParse = csvStream.ReadLine()) != null) { ++line; strArrayList.Add(CsvOperations.ParseQuotedStrings(toParse, quoteChar, delimiter, line).Skip <string>(firstColumn).Take <string>(maxColumns).ToArray <string>()); } return((IEnumerable <string[]>)strArrayList); }
public static string ToCSV(string[] textlines) { return(CsvOperations.ToCSV(textlines, '"', ',')); }
public static string[] SplitLine(string line, char quoteChar, char delimiter) { return(CsvOperations.ParseQuotedStrings(line, quoteChar, delimiter, 0).ToArray <string>()); }