Solved

Splitting Sting with Multiple Quotation Marks.

Posted on 2013-06-18
4
385 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 500 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: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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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