How can I split a Quoted String?

Hi all,

I have a string similar to this:
"1.1.1" "2.2.2 Possibly Some Info Here" "3.3.3 And Maybe Some Info Here Too!"

I would like the array to be
2.2.2 Possibly Some Info Here
3.3.3 And Maybe Some Info Here Too!

Looks like C# (2005) has no native way to do this (unless I am lissing something.

Any ideas how this can be achieved?


James AtkinSenior Principle Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richard LeeSoftware EnthusiastCommented:
Try the following code. This code assumes that your string may be prefixed and suffixed with quotation marks.


string testString = "\"1.1.1\" \"2.2.2 Possibly Some Info Here\" \"3.3.3 And Maybe Some Info Here Too!\"";
			string[] result = testString.Trim(new [] {'\"'}).Split(new [] {"\" \""}, StringSplitOptions.None);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Just use the Split() method of the string class...
Char[] _separators = new Char[] { '"' };
string[] myStrings = theString.Split(_separators, StringSplitOptions.RemoveEmptyEntries);

Open in new window

James AtkinSenior Principle Software EngineerAuthor Commented:
These both almost work..
DaTribe, if there is more than one space between the delimited items, the delimeter does not match :-(
kaylanreilor, you get blank lines in the returned array (the space(s) delimiting the string.

I can mod the result to remove the blank lines, but is there a more direct way; maybe a regex split?
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

It is because you have whitespaces between '"' in your original string. You cannot remove them since it will remove the necessary whitespaces. So I suppose that you remove afterward in the array.
Roshan DavisCommented:

Regex re = new Regex("\\s*\"");
string testString = "\"1.1.1\" \"2.2.2 Possibly Some Info Here\" \"3.3.3 And Maybe Some Info Here Too!\"";
string [] strAry = re.Split(testString);

Open in new window

Shahan AyyubSenior Software EngineerCommented:
Try this one:  

            string str = "1.1.1\" \"2.2.2 Possibly Some Info Here\" \"3.3.3 And Maybe Some Info Here Too!";
            Regex re=new Regex("\"\\s*\"");
            string[] s=re.Replace(str,"\0").Split('\0');
James AtkinSenior Principle Software EngineerAuthor Commented:
Thanks everyone for all the suggestions.
As there are several great solutions, I feel it would be right to split the points in this case - hope this is ok with everyone.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.