473,574 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Splitting string which includes quotes

VMI
How can I split a string that looks like this:

John, Doe, 37282, box 2, 10001, "My description, very important", X, Home
If I use String.Split(), it'll split the string that's between the
double-quotes, and I don't want that. How can I use the String.Split to
split the string EXCEPT the string that has double-quotes? In my case, the
separator is the comma ','.

Thanks.
Jun 20 '06 #1
4 8548
Hello VMI,

extract sting by " delimiter firstly, remove this substring from initial
one and then extract by , delimiter

V> How can I split a string that looks like this:
V>
V> John, Doe, 37282, box 2, 10001, "My description, very important", X,
V> Home
V>
V> If I use String.Split(), it'll split the string that's between the
V> double-quotes, and I don't want that. How can I use the String.Split
V> to split the string EXCEPT the string that has double-quotes? In my
V> case, the separator is the comma ','.
V>
V> Thanks.
V>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Jun 20 '06 #2
VMI wrote:
How can I split a string that looks like this:

John, Doe, 37282, box 2, 10001, "My description, very important", X, Home
If I use String.Split(), it'll split the string that's between the
double-quotes, and I don't want that. How can I use the String.Split to
split the string EXCEPT the string that has double-quotes? In my case, the
separator is the comma ','.

Thanks.


If you're not opposed to using a CSV parser, I've used this one before,
and it works pretty well:
http://www.heikniemi.net/hc/archives/000152.html

I'm pretty sure it will parse data formatted like your example perfectly.

Dan Manges
Jun 20 '06 #3
VMI wrote:
How can I split a string that looks like this:

John, Doe, 37282, box 2, 10001, "My description, very important", X, Home
If I use String.Split(), it'll split the string that's between the
double-quotes, and I don't want that. How can I use the String.Split to
split the string EXCEPT the string that has double-quotes? In my case, the
separator is the comma ','.

Thanks.


A simple lexer should do it (Note, this will strip the quotes, it can be
easily modified to leave them in, however):

public string[] GetStringParts ( string inputString )
{
List<string> retVal = new List<string>();
string currentPart = string.Empty;
int lexerState = 0;

for ( int i = 0; i < inputString.Len gth; i++ )
{
switch ( lexerState )
{
case 0:
if ( inputString[i] == ',' )
{
retVal.Add( currentPart.Tri m() );
currentPart = string.Empty;
}
else if ( inputString[i] == '"' )
lexerState = 1;
else
currentPart += inputString[i];
break;

case 1:
if ( inputString[i] == '"' )
lexerState = 0;
else
currentPart += inputString[i];
break;
}
}

return retVal.ToArray( );
}

Hope this helps,
-- Tom Spink
Jun 20 '06 #4
Tom Spink wrote:
VMI wrote:
How can I split a string that looks like this:

John, Doe, 37282, box 2, 10001, "My description, very important", X, Home
If I use String.Split(), it'll split the string that's between the
double-quotes, and I don't want that. How can I use the String.Split to
split the string EXCEPT the string that has double-quotes? In my case,
the separator is the comma ','.

Thanks.


A simple lexer should do it (Note, this will strip the quotes, it can be
easily modified to leave them in, however):

public string[] GetStringParts ( string inputString )
{
List<string> retVal = new List<string>();
string currentPart = string.Empty;
int lexerState = 0;

for ( int i = 0; i < inputString.Len gth; i++ )
{
switch ( lexerState )
{
case 0:
if ( inputString[i] == ',' )
{
retVal.Add( currentPart.Tri m() );
currentPart = string.Empty;
}
else if ( inputString[i] == '"' )
lexerState = 1;
else
currentPart += inputString[i];
break;

case 1:
if ( inputString[i] == '"' )
lexerState = 0;
else
currentPart += inputString[i];
break;
}
}

return retVal.ToArray( );
}

Hope this helps,
-- Tom Spink


Ha, I spotted a bug the moment I clicked Send:

Before the line:
return retVal.ToArray( );

Add this:
if ( currentPart != string.Empty )
retVal.Add( currentPart );

Without that modification, the last entry would not get added to the return
array.

Whoops!
-- Tom Spink
Jun 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
14967
by: J. W. McCall | last post by:
Hello, I need to split a string as per string.strip(), but with a modification: I want it to recognize quoted strings and return them as one list item, regardless of any whitespace within the quoted string. For example, given the string: 'spam "the life of brian" 42'
6
2108
by: tshad | last post by:
In my User control, I tried to do this: *************************************************************************** <Script runat="server"> Public ClientName As String = "<!-- #include file = ...\includes\StaffingHeaders.inc -->" </Script> <%=ClientName%> ****************************************************************************
2
2511
by: Trint Smith | last post by:
Ok, My program has been formating .txt files for input into sql server and ran into a problem...the .txt is an export from an accounting package and is only supposed to contain comas (,) between fields in a table...well, someone has been entering description fields with comas (,) in the description and now it is splitting between one...
5
2338
by: sridev | last post by:
hi all, i have a xml file.I have converted the xml file to String using OuterXml....... now i am getting like this "<ClientList><Client action=\"Modify\"><ClientID>nokia</ClientID><FullName>Raj</FullName><AccountNo>12345</AccountNo><Type>Individual </Type><Source>By client referral </Source><Status>Bad
2
1477
by: CharChabil | last post by:
Using Vb.net 2005, I want to read each part in this string in an array (splitting the string) ----------- A1/EXT "BK82 LB73 21233" 105 061018 1804 ----------- That Code that i used is as follow: Dim s As String, h As String Dim delim(1) As Char delim(0) = "/"
3
1697
by: MSNEWS | last post by:
Hi i need to extract all items between commas into a string array from the following line, but there is a comma in one of the items (between strings) eg "MICHEAL JACKSON, AAMC" , i used String.Split() method but it fails. 9006374,7912103,0,"MICHEAL JACKSON, AAMC","102-3085146-8388914" can anyone help out
6
303
by: HMS Surprise | last post by:
The string s below has single and double qoutes in it. For testing I surrounded it with triple single quotes. I want to split off the portion before the first \, but my split that works with shorter strings does not seem to work with this one. Ideas? Thanks, jvh
5
2039
by: mosscliffe | last post by:
I am looking for a simple split function to create a list of entries from a string which contains quoted elements. Like in 'google' search. eg string = 'bob john "johnny cash" 234 june' and I want to have a list of I wondered about using the csv routines, but I thought I would ask the experts first.
9
8823
by: conspireagainst | last post by:
I'm having quite a time with this particular problem: I have users that enter tag words as form input, let's say for a photo or a topic of discussion. They are allowed to delimit tags with spaces and commas, and can use quotes to encapsulate multiple words. An example: tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6" So, as we can...
0
7808
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...
0
7732
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...
1
7822
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...
0
6456
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...
0
5302
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3742
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3754
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1062
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.