"csv parser c#" Code Answer's

You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "csv parser c#" answered properly. Developers are finding an appropriate answer about csv parser c# related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like csv parser c#. Enter your desired code related query in the search bar and get every piece of information about C# code related question on csv parser c#. 

csv parser c#

By Inexpensive IbexInexpensive Ibex on Jan 21, 2021
public static class CsvParser
{
    private static Tuple<T, IEnumerable<T>> HeadAndTail<T>(this IEnumerable<T> source)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        var en = source.GetEnumerator();
        en.MoveNext();
        return Tuple.Create(en.Current, EnumerateTail(en));
    }

    private static IEnumerable<T> EnumerateTail<T>(IEnumerator<T> en)
    {
        while (en.MoveNext()) yield return en.Current;
    }

    public static IEnumerable<IList<string>> 
           Parse(string content, char delimiter, char qualifier)
    {
        using (var reader = new StringReader(content))
            return Parse(reader, delimiter, qualifier);
    }

    public static Tuple<IList<string>, IEnumerable<IList<string>>> 
           ParseHeadAndTail(TextReader reader, char delimiter, char qualifier)
    {
        return HeadAndTail(Parse(reader, delimiter, qualifier));
    }

    public static IEnumerable<IList<string>> 
           Parse(TextReader reader, char delimiter, char qualifier)
    {
        var inQuote = false;
        var record = new List<string>();
        var sb = new StringBuilder();

        while (reader.Peek() != -1)
        {
            var readChar = (char) reader.Read();

            if (readChar == '\n' || (readChar == '\r' && (char) reader.Peek() == '\n'))
            {
                // If it's a \r\n combo consume the \n part and throw it away.
                if (readChar == '\r')
                    reader.Read();

                if (inQuote)
                {
                    if (readChar == '\r')
                        sb.Append('\r');
                    sb.Append('\n');
                }
                else
                {
                    if (record.Count > 0 || sb.Length > 0)
                    {
                        record.Add(sb.ToString());
                        sb.Clear();
                    }

                    if (record.Count > 0)
                        yield return record;

                    record = new List<string>(record.Count);
                }
            }
            else if (sb.Length == 0 && !inQuote)
            {
                if (readChar == qualifier)
                    inQuote = true;
                else if (readChar == delimiter)
                {
                    record.Add(sb.ToString());
                    sb.Clear();
                }
                else if (char.IsWhiteSpace(readChar))
                {
                    // Ignore leading whitespace
                }
                else
                    sb.Append(readChar);
            }
            else if (readChar == delimiter)
            {
                if (inQuote)
                    sb.Append(delimiter);
                else
                {
                    record.Add(sb.ToString());
                    sb.Clear();
                }
            }
            else if (readChar == qualifier)
            {
                if (inQuote)
                {
                    if ((char) reader.Peek() == qualifier)
                    {
                        reader.Read();
                        sb.Append(qualifier);
                    }
                    else
                        inQuote = false;
                }
                else
                    sb.Append(readChar);
            }
            else
                sb.Append(readChar);
        }

        if (record.Count > 0 || sb.Length > 0)
            record.Add(sb.ToString());

        if (record.Count > 0)
            yield return record;
    }
}

Source: www.codeproject.com

Add Comment

0

All those coders who are working on the C# based application and are stuck on csv parser c# can get a collection of related answers to their query. Programmers need to enter their query on csv parser c# related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about csv parser c# for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of csv parser c# while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "csv parser c#". Visit this developer's friendly online web community, CodeProZone, and get your queries like csv parser c# resolved professionally and stay updated to the latest C# updates. 

C# answers related to "csv parser c#"

View All C# queries

C# queries related to "csv parser c#"

Browse Other Code Languages

CodeProZone