472,122 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 8351
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.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
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.

Similar topics

7 posts views Thread by J. W. McCall | last post: by
6 posts views Thread by tshad | last post: by
2 posts views Thread by Trint Smith | last post: by
2 posts views Thread by CharChabil | last post: by
3 posts views Thread by MSNEWS | last post: by
6 posts views Thread by HMS Surprise | last post: by
5 posts views Thread by mosscliffe | last post: by
reply views Thread by leo001 | last post: by

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.