user2739418 February 2016

.net split - without double quote

I have an CSV file as below:

ID,Name,Address,PhoneNumber
101,Jack,"No 13, HillTop, London",012346789
102,Harry,"No 15, Baker Street London",012346789

I need to read all the columns (Comma separated). When I use split function it splits address as well. I want to split anything except the address which is in double quote.

Answers


Dave February 2016

Even though you are using C#, there is a very useful class called TextFieldParser in the Microsoft.VisualBasic namespace. You will need to add a reference to your project in addition to the using directive:

using Microsoft.VisualBasic.FileIO;

Then you can implement something similar to as follows:

private void Parse()
{
    using (TextFieldParser parser = new TextFieldParser("file.csv")
    {
        HasFieldsEnclosedInQuotes = true,
        Delimiters = new string[] {
            ","
        }
    })
    {
        string[] fields;
        do
        {
            fields = parser.ReadFields();
            PrintResults(fields);
        }
        while (fields != null);
    }
}

private void PrintResults(string[] fields)
{
    if (fields != null)
    {
        foreach (var field in fields)
        {
            Console.Write(string.Concat("[", field, "] "));
        }
        Console.WriteLine();
    }
}

The HasFieldsEnclosedInQuotes = true property of the TextFieldParser in your case must be set to achieve desired behavior.

I have placed your CSV sample data into a file and run as a test. The data I started out with was (in a local file named "file.csv"):

ID,Name,Address,PhoneNumber
101,Jack,"No 13, HillTop, London",012346789
102,Harry,"No 15, Baker Street London",012346789

And the resultant output in the console from calling the above Parse() method is:

[ID] [Name] [Address] [PhoneNumber] 
[101] [Jack] [No 13, HillTop, London] [012346789] 
[102] [Harry] [No 15, Baker Street London] [012346789] 


tede24 February 2016

An alternative to using suggested parsing library, could be using REGEX.

The expression needed to parse this string, considering the "xx,xx" case is (".*"|.*?)(,|$).

A sample code using it in C#:

//preparation
var pattern = @"("".*""|.*?)(,|$)";
var regex = new Regex(pattern);

//for each file line
var text =@"101,Jack,""No 13, HillTop, London"",0123456789";
var matches = regex.Matches(text).Cast<Match>().Select(m=>m.Groups[1].Value);

Post Status

Asked in February 2016
Viewed 2,893 times
Voted 4
Answered 2 times

Search




Leave an answer