473,802 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Split string with quotes in it

3 New Member
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.Spli t(delimiterChar s);

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 5892
SammyB
807 Recognized Expert Contributor
You'll need to use RegularExpressi ons, 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 Recognized Expert Contributor
You'll need to use RegularExpressi ons, 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
3281
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 the space character (or tab or newline). ex... $tklist = preg_split("/\s|\n|\t/", $str); This works except in the string case, where "HELLO WORLD!" should not be two parts. How do I split by spaces but keep phrases inside quotes
5
3272
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 list. assume the string is in the variable f, then i tried f.split() but i end up with
4
728
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 ignore pairs of brackets like: "one" <tab> "two three" ( four "five six" ) Want fields like:
6
6384
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
3
2103
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; // Edward Two questions about this: 1. Why do you use single quotes with Split() instead of double? Is this
8
2749
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
1776
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 I can't figure out how to escape a quote. The closest I got was Regex.split(String,"(\"")"), which gives me an array of strings: "X:Y\Z.exe
14
4723
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. function csvToArray (f /* file handle */) { // convert csv file to Javascript 2d array (array of arrays) // written in Javascript but file lib functions are idealized var array2d = new Array(0);
5
4780
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 call. If some element contains commas, I don't want to split it. Examples: 'foo, bar, baz' ='foo' 'bar' 'baz'
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10285
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.