473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Split string with quotes in it

Hi
I want to split a string into an array and treat quoted phrases as words.

I was splitting on a space which worked fine.

char[] delimiterChars = {' '};
string[] arrValues = searchText.Split(delimiterChars);

This would split a sentence ...
this is a test
into an array of size 4.

But I want to split and preserve the data in double qoutes.
i.e. it treats anything inside two quotes as a word

this is a "really really long" test

would create an array of size five i.e.

this
is
a
really really long
test

Any ideas?
Thanks!
P
May 11 '07 #1
2 5851
SammyB
807 Expert 512MB
You'll need to use RegularExpressions, http://msdn2.microsoft.com/en-us/lib...gex.split.aspx. If you google regex split excel, you'll find code like:
Expand|Select|Wrap|Line Numbers
  1. string input = "john,doe,is,\"123 somewhere, suite 4\"";
  2. string pattern = ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
  3. string[] items = System.Text.RegularExpressions.Regex.Split(input, pattern);
It's just ugly enough to work, but it splits on commas instead of spaces. I thought that I had a sample that splits on spaces or commas, but I cannot find it. If you google some more, you may find it; otherwise, you'll need a regex expert, not me.
May 11 '07 #2
SammyB
807 Expert 512MB
You'll need to use RegularExpressions, http://msdn2.microsoft.com/en-us/lib...gex.split.aspx. If you google regex split excel, you'll find code like:
Expand|Select|Wrap|Line Numbers
  1. string input = "john,doe,is,\"123 somewhere, suite 4\"";
  2. string pattern = ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
  3. string[] items = System.Text.RegularExpressions.Regex.Split(input, pattern);
It's just ugly enough to work, but it splits on commas instead of spaces. I thought that I had a sample that splits on spaces or commas, but I cannot find it. If you google some more, you may find it; otherwise, you'll need a regex expert, not me.
Found my code, it's the same as what I posted, so it does not split on spaces. Sorry.

You also might want to consider what you want to to do with "a""b"
Excel parses this to a single cell containing a"b
May 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: afrinspray | last post by:
I'm writing a function that parses a nested list string that might look like this: ( "HELLO WORLD!" 1231231 awesome ( 1 2 ) ) I wrote the logic already and it starts by splitting the string by...
5
by: oliver | last post by:
hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a...
4
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
3
by: John Salerno | last post by:
This is an example in the book I'm reading: string fullName = " Edward C Koop "; fullName = fullName.Trim(); string names = fullName.Split(' '); string firstName = names; //...
8
by: mannyGonzales | last post by:
Hey guys, Earliery I posted this common task of reading a csv file. My data read as: "1","2","3" Unfortunately it now reads as: "1","Text with, comma", "2" embedded commas!...
5
by: lgbjr | last post by:
Hello All, I have the following type of string: "X:Y\Z.exe" "123" What I need is an array of strings with the information from within each set of quotes. I was trying to use a Regex.Split, but...
14
by: tom t/LA | last post by:
Here is a function to convert a CSV file to a Javascript array. Uses idealized file reading functions based on the std C library, since there is no Javascript standard. Not fully tested. ...
5
by: Robert Dodier | last post by:
Hello, I'd like to split a string by commas, but only at the "top level" so to speak. An element can be a comma-less substring, or a quoted string, or a substring which looks like a function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.