Solved

Splitting Sting with Multiple Quotation Marks.

Posted on 2013-06-18
4
380 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 75

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Connecting database in Xamarin? 6 48
Unable to upgrade to .Net 4.6.2 on Server 2012r2 2 42
Help with C#, MVC, razor. 6 18
Exit the loop 4 22
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
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.

932 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