Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Splitting Sting with Multiple Quotation Marks.

Posted on 2013-06-18
4
Medium Priority
?
389 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 75

Accepted Solution

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

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:Meir Rivkin
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

618 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