CSV to List

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    CSV to List

    Hello,

    How can I create a List of String from a CSV String?

    For example, from:

    "New York, London ,Lisbon ,Car,C# "

    I would get a list with the following items:
    "New York", "London", "Lisbon", "Car", "C#"

    So I would take all words but ignore the spaces before and after a
    comma but not the ones between words.

    Should I make this with Regex?

    Thanks,
    Miguel
  • Peter Duniho

    #2
    Re: CSV to List

    On Thu, 26 Jun 2008 17:29:48 -0700, shapper <mdmoura@gmail. comwrote:
    How can I create a List of String from a CSV String?
    >
    [...]
    Should I make this with Regex?
    I suppose you could. But IMHO, the Split() and Trim() methods of String
    provide everything you need:

    public List<stringCSVT oList(string strInput)
    {
    string[] rgstrRecord = strInput.Split( new char[] { ',' });
    List<stringlstr Ret = new List<string>(rg strRecord.Lengt h);

    foreach (string strColumn in rgstrRecord)
    {
    lstrRet.Add(str Column.Trim());
    }

    return lstrRet;
    }

    No doubt there's a LINQ way to do all that that looks much nicer, but I've
    been slacking and still can't write it off the top of my head. I'll learn
    LINQ one day. :)

    Pete

    Comment

    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

      #3
      Re: CSV to List

      shapper wrote:
      How can I create a List of String from a CSV String?
      >
      For example, from:
      >
      "New York, London ,Lisbon ,Car,C# "
      >
      I would get a list with the following items:
      "New York", "London", "Lisbon", "Car", "C#"
      >
      So I would take all words but ignore the spaces before and after a
      comma but not the ones between words.
      Fundamentally:
      Split
      Trim
      assign

      C# 2.0:

      public static List<stringSpec ialSplit20(stri ng s)
      {
      List<stringres = new List<string>();
      foreach(string part in s.Split(','))
      {
      res.Add(part.Tr im());
      }
      return res;
      }

      C# 3.5:

      public static List<stringSpec ialSplit30(stri ng s)
      {
      return new List<string>(fr om part in s.Split(',') select
      part.Trim());
      }

      Arne

      Comment

      • Jay Riggs

        #4
        Re: CSV to List

        On Jun 26, 5:29 pm, shapper <mdmo...@gmail. comwrote:
        Hello,
        >
        How can I create a List of String from a CSV String?
        >
        For example, from:
        >
        "New York, London   ,Lisbon    ,Car,C#    "
        >
        I would get a list with the following items:
        "New York", "London", "Lisbon", "Car", "C#"
        >
        So I would take all words but ignore the spaces before and after a
        comma but not the ones between words.
        >
        Should I make this with Regex?
        >
        Thanks,
        Miguel

        Miguel,


        There are a number of CSV readers freely available. Here's my
        favorite:


        I believe this one can read directly into a List<stringand includes:


        -Jay

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: CSV to List

          On Jun 27, 1:44 am, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
          wrote:
          No doubt there's a LINQ way to do all that that looks much nicer, but I've  
          been slacking and still can't write it off the top of my head.  I'll learn  
          LINQ one day.  :)
          strInput.Split( ',').Select(x =x.Trim()).ToLi st()

          :)

          Jon

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: CSV to List

            On Jun 27, 1:52 am, Arne Vajhøj <a...@vajhoej.d kwrote:

            <snip>

            Just wearing my version number pedantry hat:
            C# 3.5:
            It's C# 3, not 3.5. I've got an article about the version numbers at


            (I know I've been doing a lot of this recently. I can understand if
            it's slightly annoying, but I think it's for the best in the long run.
            If other groupies would rather I stopped, please let me know.)
                     public static List<stringSpec ialSplit30(stri ng s)
                     {
                         return new List<string>(fr om part in s.Split(',') select
            part.Trim());
                     }
            I've just posted my version as a reply to Pete's post - I tend not to
            bother with a query expression if I'm just using one operator (just
            select, or a where followed by a no-op select). And ToList is
            goodness :)

            Jon

            Comment

            • Peter Duniho

              #7
              Re: CSV to List

              On Thu, 26 Jun 2008 22:54:58 -0700, Jon Skeet [C# MVP] <skeet@pobox.co m>
              wrote:
              Just wearing my version number pedantry hat:
              >
              >C# 3.5:
              >
              It's C# 3, not 3.5. I've got an article about the version numbers at

              >
              (I know I've been doing a lot of this recently. I can understand if
              it's slightly annoying, but I think it's for the best in the long run.
              If other groupies would rather I stopped, please let me know.)
              For what it's worth, I find the version corrections useful, even if it's
              unlikely I'm ever going to get everything memorized. If anything, as more
              incremental versions come out, and as there become more combinations of
              CLR, C#, and .NET versions available, I think it's more and more important
              to be precise about which features require which versions.

              Heck, just the other day I had to unconfuse myself about which .NET
              versions include the various Action delegates, since it turns out two
              showed up in 3.0 and the other three showed up in 3.5! I find it helpful
              to have other folks piping up to keep me honest.

              That said, I realize opinions may vary. That's just my two cents. :)

              Pete

              Comment

              • Marc Gravell

                #8
                Re: CSV to List

                about which .NET
                versions include the various Action delegates, since it turns out two
                showed up in 3.0 and the other three showed up in 3.5!
                2.0, surely? I could well be wrong, but Action<Tis used in
                List<T>.ForEach since 2.0?

                Marc

                Comment

                • Marc Gravell

                  #9
                  Re: CSV to List

                  Just wearing my version number pedantry hat:

                  Now if only we could fix the number of books touting C# 2008 ;-p

                  Marc

                  Comment

                  • Peter Duniho

                    #10
                    Re: CSV to List

                    On Fri, 27 Jun 2008 00:27:05 -0700, Marc Gravell <marc.gravell@g mail.com>
                    wrote:
                    >about which .NET
                    >versions include the various Action delegates, since it turns out two
                    >showed up in 3.0 and the other three showed up in 3.5!
                    >
                    2.0, surely? I could well be wrong, but Action<Tis used in
                    List<T>.ForEach since 2.0?
                    Yes, sorry. Typo.

                    However, I was mistaken. Only Action<Twas in 2.0. The rest all appear
                    to not have shown up until 3.5 (I mistakenly assumed that the
                    parameterless Action type had been around as long as Action<T>).

                    Pete

                    Comment

                    • shapper

                      #11
                      Re: CSV to List

                      On Jun 27, 8:57 am, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
                      wrote:
                      On Fri, 27 Jun 2008 00:27:05 -0700, Marc Gravell <marc.grav...@g mail.com> 
                      wrote:
                      >
                      about which .NET
                      versions include the various Action delegates, since it turns out two
                      showed up in 3.0 and the other three showed up in 3.5!
                      >
                      2.0, surely? I could well be wrong, but Action<Tis used in
                      List<T>.ForEach since 2.0?
                      >
                      Yes, sorry.  Typo.
                      >
                      However, I was mistaken.  Only Action<Twas in 2.0.  The rest all appear  
                      to not have shown up until 3.5 (I mistakenly assumed that the  
                      parameterless Action type had been around as long as Action<T>).
                      >
                      Pete
                      Thank you all!

                      For now I think I will use the Linq version.
                      It is short and it solves my particular problem.

                      But the other suggestions might be handy in the future.

                      Thank You,
                      Miguel

                      Comment

                      • Peter Duniho

                        #12
                        Re: CSV to List

                        On Fri, 27 Jun 2008 04:24:20 -0700, shapper <mdmoura@gmail. comwrote:
                        For now I think I will use the Linq version.
                        It is short and it solves my particular problem.
                        >
                        But the other suggestions might be handy in the future.
                        Except for possibly Jay's, probably not. Jon's LINQ suggestion is
                        essentially identical to the code examples posted to this thread. It's
                        just a lot more concise. :)

                        Pete

                        Comment

                        Working...