Solved

Splitting Sting with Multiple Quotation Marks.

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# bracket error 3 30
REXEX help Part 2 2 23
Send SMS from VB via Sierra Wireless Modem 2 21
Need help with a Crystal Report in my C#.Net application 2 14
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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