Solved

Splitting Sting with Multiple Quotation Marks.

Posted on 2013-06-18
4
378 Views
Last Modified: 2013-06-27
Hi, I'm using C#.NET 2010. -- I have a Sting that looks much like the format below.
I need to split it on the following three characters >   ","   < .  I cannot simply split it on the comma, because some of the parts of the Sting has commas in it.

Example:

"item_1, item_2, item_3, and item_4","item_5 and item_6, and item_7","item_8, item_9, item10"

So, in essence I have 3 basic parts to the larger String:

string1 = "item_1, item_2, item_3, and item_4"
string2 = "item_5 and item_6, and item_7"
string3 = "item_8, item_9, item10"

All 3 sub-strings are enclosed with quotation marks (") and separated by a comma (,).

How do I split on ---   ","  ?

I tried --  ' \",\" '    and    ' " , " '   and also   @","    but none of those worked.

Can someone please assist?

Thank you,
Fulano
0
Comment
Question by:Mr_Fulano
4 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39258102
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 39258222
There are many exsisting good CSV parsers that you can use. E.g.,  Microsoft.VisualBasic.FileIO.TextFieldParser (you need to add a reference).

E.g., you have a simple file test.csv:
-------------------------------------------
id, description, quantity
1,"hello, world", 1000
2, "a,b,c",2000
---------------------------------------------------

Sample code:

        private void button4_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser("test.csv");
            parser.SetDelimiters(new string[] { "<", ",", ">" });
            parser.HasFieldsEnclosedInQuotes = true;

            int counter = 0;
            while (true) // read line by line and check number of fields 
            {
                counter += 1;
                string[] fields = parser.ReadFields();
                if (fields == null)
                {
                    break;
                }
                // just print how many fields in each line
                System.Diagnostics.Debug.WriteLine("Line {0}: {1} field(s)", counter, fields.Length);
            }
        }

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39258498
here an example:

 string str = "\"item_1, item_2, item_3, and item_4\",\"item_5 and item_6, and item_7\",\"item_8, item_9, item10\"";
var tokens = str.Split('"').Where((s, i) => i % 2 != 0);

Open in new window

0
 

Author Closing Comment

by:Mr_Fulano
ID: 39283255
Good suggestion. Thank you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now