Splitting string which includes quotes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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 Nietzsch
 
VMI said:
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
 
VMI said:
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 == ',' )
{
retVal.Add( currentPart.Trim() );
currentPart = string.Empty;
}
else if ( inputString == '"' )
lexerState = 1;
else
currentPart += inputString;
break;

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

return retVal.ToArray();
}

Hope this helps,
-- Tom Spink
 
Tom said:
VMI said:
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 == ',' )
{
retVal.Add( currentPart.Trim() );
currentPart = string.Empty;
}
else if ( inputString == '"' )
lexerState = 1;
else
currentPart += inputString;
break;

case 1:
if ( inputString == '"' )
lexerState = 0;
else
currentPart += inputString;
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top