Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Splitting comma-seperated items

Reply
Thread Tools

Splitting comma-seperated items

 
 
MSNEWS
Guest
Posts: n/a
 
      04-21-2007
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

TIA
Barry



 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      04-21-2007
"MSNEWS" <(E-Mail Removed)> wrote in message
news:%23pq5R8$(E-Mail Removed)...

> 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


Will there always be 5 elements in the string? I.e.

9006374
7912103
0
"MICHEAL JACKSON, AAMC"
"102-3085146-8388914"

If so, then it's a fairly trivial exercise:

string strLineIn = <read in your line>;
List<string> lstElements = new List<string>(strLineIn.Split(','));
string strElement1 = lstElements[0];
string strElement2 = lstElements[1];
string strElement3 = lstElements[2];
string strElement5 = lstElements[lstElements.Count - 1];
lstElements.RemoveAt(0);
lstElements.RemoveAt(0);
lstElements.RemoveAt(0);
lstElements.RemoveAt(lstElements.Count - 1);
string strElement4 = String.Empty;
foreach (string strElement in lstElements)
{
strElement4 += strElement + ",";
}
strElement4 = strElement4.TrimEnd(',');

N.B. there are almost certainly cleverer / more efficient ways of doing
this...


 
Reply With Quote
 
 
 
 
Chris Taylor
Guest
Posts: n/a
 
      04-21-2007
Hi,

You can try the following regular expression

Regex regex = new
Regex("((?<field>[^\",\\r\\n]+)|\"(?<field>([^\"]|\"\")+)\")(,|(?<rowbreak>\\r\\n|\\n|$))");
foreach(Match match in regex.Matches(s))
{
Console.WriteLine(match.Value);
}

The result will include the comma and the quotes as follows

9006374,
7912103,
0,
"MICHEAL JACKSON, AAMC",
"102-3085146-8388914"

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor


"MSNEWS" <(E-Mail Removed)> wrote in message
news:%23pq5R8$(E-Mail Removed)...
> 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
>
> TIA
> Barry
>
>
>


 
Reply With Quote
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      04-21-2007
Regex is a great way to go. I would say that an alteration of the method, to
change the commas not in the middle of quotes, so you could split, would be
faster for what he is trying to do.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*********************************************
Think outside the box!
*********************************************
"Chris Taylor" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> You can try the following regular expression
>
> Regex regex = new
> Regex("((?<field>[^\",\\r\\n]+)|\"(?<field>([^\"]|\"\")+)\")(,|(?<rowbreak>\\r\\n|\\n|$))");
> foreach(Match match in regex.Matches(s))
> {
> Console.WriteLine(match.Value);
> }
>
> The result will include the comma and the quotes as follows
>
> 9006374,
> 7912103,
> 0,
> "MICHEAL JACKSON, AAMC",
> "102-3085146-8388914"
>
> Hope this helps
>
> --
> Chris Taylor
> http://dotnetjunkies.com/weblog/chris.taylor
>
>
> "MSNEWS" <(E-Mail Removed)> wrote in message
> news:%23pq5R8$(E-Mail Removed)...
>> 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
>>
>> TIA
>> Barry
>>
>>
>>

>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Splitting a line while keeping quoted items together Tim Chase Python 0 11-20-2012 01:20 AM
Splitting a line while keeping quoted items together josh@merchantconcepts.com Python 1 11-20-2012 01:09 AM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments