By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,483 Members | 3,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,483 IT Pros & Developers. It's quick & easy.

Splitting string which includes quotes

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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.Length; i++ )
{
switch ( lexerState )
{
case 0:
if ( inputString[i] == ',' )
{
retVal.Add( currentPart.Trim() );
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

P: n/a
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.Length; i++ )
{
switch ( lexerState )
{
case 0:
if ( inputString[i] == ',' )
{
retVal.Add( currentPart.Trim() );
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 discussion thread is closed

Replies have been disabled for this discussion.