0

Im trying to split a string into sub parts to pass into a linq database, but ive come up with a problem. The file is a .csv file so its split up by commas example :

1,Ms,Aleshia,Tomkiewicz,14 Taylor St,St. Stephens Ward,Kent,CT2 7PP,01835-703597,[email protected].

However some of the data contains commas in the data field like a county/address is split up with a comma however i dont want it to split i want it to keep that data all together for example address: London,Wimbledon.

im using this code currently to do the chopping:


 public static List<string> ReturnCSVFromWeb(string url)
        {
            System.Net.WebClient client = new WebClient();
            string CSVContent = client.DownloadString(url);

            List<string> splitted = new List<string>();
            string csvFile = CSVContent;
            string[] tempStr;

            tempStr = csvFile.Split(',','\n');

            foreach (string item in tempStr)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    splitted.Add(item);
                }
            }

            return splitted;

        }
10
  • 3
    Please Stop Rolling Your Own CSV Parser. Also respect a standard format for your data. You don't seem to have CSV but rather some custom format if London,Wimbledon should not be separated. You should be thinking of how to properly encode your data in the first place. Commented Nov 29, 2015 at 15:54
  • not allowed to use 3rd party software only standard .Net libraries Commented Nov 29, 2015 at 15:57
  • 2
    Then better be prepared to descend to the abyss. And please don't tag your question with CSV because clearly that's not the format that you have as input. rather specify that you have some custom format that somebody invented and make sure that you define very precisely this format before trying to parse it. Commented Nov 29, 2015 at 15:57
  • Are you sure that the address values are not just individual columns? Commented Nov 29, 2015 at 15:59
  • sorry i meant we have an address like this in one field :Broxburn, Uphall and Winchburg which is sperated by commas Commented Nov 29, 2015 at 15:59

2 Answers 2

6

There is no way to solve this problem unless you know ahead of time which data contains commas. A better option would be to have each entry in the csv surrounded by double quote, and then seperated by comma

Sign up to request clarification or add additional context in comments.

3 Comments

hmmm thought so but we are not allowed to alter the data as part of a coursework :/
sorry for bad description the csv file is like this : 14,Mr,Niesha,Bruch,24 Bolton St,"Broxburn, Uphall and Winchburg",West Lothian,EH52 notice how "Broxburn , Uphall" has a comma in the address
Notice how that string is enclosed in double quotes. That makes it standard CSV. Why did you omit the quotes in the original post? Did you not see them as significant?
2

If number of rows is fixed (M) and only address column has comma, you can do this:

  1. Split row
  2. Take first N1 columns before address
  3. Take last N2 columns after address
  4. Take M-N1-N2 columns from middle, join them - it's an address

3 Comments

This assumes of course that only one column contains a comma in the value
sorry for bad description the csv file is like this : 14,Mr,Niesha,Bruch,24 Bolton St,"Broxburn, Uphall and Winchburg",West Lothian,EH52 notice how "Broxburn , Uphall" has a comma in the address
@hpearson split by " first - you'll get an address, then split other parts by ,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.