Splitting comma-seperated items

Discussion in 'ASP .Net' started by MSNEWS, Apr 21, 2007.

  1. MSNEWS

    MSNEWS Guest

    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
    MSNEWS, Apr 21, 2007
    #1
    1. Advertising

  2. MSNEWS

    Mark Rae Guest

    "MSNEWS" <> wrote in message
    news:%23pq5R8$...

    > 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...
    Mark Rae, Apr 21, 2007
    #2
    1. Advertising

  3. MSNEWS

    Chris Taylor Guest

    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" <> wrote in message
    news:%23pq5R8$...
    > 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
    >
    >
    >
    Chris Taylor, Apr 21, 2007
    #3
  4. 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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:%23pq5R8$...
    >> 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
    >>
    >>
    >>

    >
    Cowboy \(Gregory A. Beamer\), Apr 21, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: splitting field by comma

    Kevin Spencer, May 19, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    450
    =?Utf-8?B?UGFvbGE=?=
    May 20, 2004
  2. steven
    Replies:
    2
    Views:
    596
    steven
    Oct 26, 2003
  3. Kamyk
    Replies:
    0
    Views:
    358
    Kamyk
    Oct 7, 2004
  4. Pmb
    Replies:
    2
    Views:
    373
  5. Stan R.
    Replies:
    6
    Views:
    447
    Joe Kesselman
    Jun 5, 2006
Loading...

Share This Page