convert generic string list to one string

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

    convert generic string list to one string

    is there any built in function or dotnet framework(versi on 2) to merge a
    generic list of string into one string with each element delimited by
    specified delimiting string?

    or do I have to roll my own/ IT is not hard to roil my own but hate to
    re-invent the wheel. I have searched built-in help. Google but failed to
    used the right search term to come anything good




  • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

    #2
    RE: convert generic string list to one string

    Try using the CopyTo method, and then taking the String.Join method against
    that.
    -- Peter
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    MetaFinder: http://www.blogmetafinder.com


    "gs" wrote:
    is there any built in function or dotnet framework(versi on 2) to merge a
    generic list of string into one string with each element delimited by
    specified delimiting string?
    >
    or do I have to roll my own/ IT is not hard to roil my own but hate to
    re-invent the wheel. I have searched built-in help. Google but failed to
    used the right search term to come anything good
    >
    >
    >
    >
    >

    Comment

    • Marc Gravell

      #3
      Re: convert generic string list to one string

      One line?
      string value = string.Join(sep arator, list.ToArray()) ;

      Although from a purist view it would be more efficient to iterate on
      the values themselves (rather than forcing an array for no real
      reason). You can do this with a simple utility method, or in C# 3,
      perhaps an extension method (although "Join" is a poor choice of name,
      as it might clash with LINQ's Join):

      string value = someSetOfString s.Join(separato r); // example
      usage
      ...
      public static string Join(this IEnumerable<str ingvalues,
      string separator) {
      StringBuilder sb = new StringBuilder() ;
      bool first = true;
      foreach (string value in values) {
      if (first) {
      first = false;
      } else {
      sb.Append(separ ator);
      }
      sb.Append(value );
      }
      return sb.ToString();
      }

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: convert generic string list to one string

        Marc Gravell <marc.gravell@g mail.comwrote:
        One line?
        string value = string.Join(sep arator, list.ToArray()) ;
        >
        Although from a purist view it would be more efficient to iterate on
        the values themselves (rather than forcing an array for no real
        reason). You can do this with a simple utility method, or in C# 3,
        perhaps an extension method (although "Join" is a poor choice of name,
        as it might clash with LINQ's Join):
        >
        string value = someSetOfString s.Join(separato r); // example
        usage
        ...
        public static string Join(this IEnumerable<str ingvalues,
        string separator) {
        StringBuilder sb = new StringBuilder() ;
        bool first = true;
        foreach (string value in values) {
        if (first) {
        first = false;
        } else {
        sb.Append(separ ator);
        }
        sb.Append(value );
        }
        return sb.ToString();
        }
        This is a great case for Aggregate:

        words.Aggregate (new StringBuilder() ,
        (sb, word) =sb.Append(word ).Append(separa tor),
        sb =sb.ToString()) ;

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        World class .NET training in the UK: http://iterativetraining.co.uk

        Comment

        • Marc Gravell

          #5
          Re: convert generic string list to one string

          This is a great case for Aggregate:

          Well, one too many separators, but a tidy job I must admit.
          Of course, it fails the "dotnet framework(versi on 2)" test, but then
          my C# 3 and .NET 2 [sp1] offering probably isn't what the OP wanted
          either ;-p

          Marc

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: convert generic string list to one string

            Marc Gravell <marc.gravell@g mail.comwrote:
            This is a great case for Aggregate:
            >
            Well, one too many separators, but a tidy job I must admit.
            Rats. Hmm. Probably easiest to do the "add the separator at the start
            but only if the builder isn't empty" trick to avoid that.
            Of course, it fails the "dotnet framework(versi on 2)" test, but then
            my C# 3 and .NET 2 [sp1] offering probably isn't what the OP wanted
            either ;-p
            :)

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            World class .NET training in the UK: http://iterativetraining.co.uk

            Comment

            • gs

              #7
              Re: convert generic string list to one string

              thank you all for the input and insights.

              I already rolled one along the line mentioned by Marc, starting with empty
              string and iterate through the list


              hopefully may be one of the future version of c# will provide built-in
              function to do what I did to increase productivity. I will not expect that
              to happen for compact framework but I do hope it will for the regular .net
              for windows.



              "gs" <gs@dontMail.te luswrote in message
              news:edPNRqPPIH A.1204@TK2MSFTN GP03.phx.gbl...
              is there any built in function or dotnet framework(versi on 2) to merge a
              generic list of string into one string with each element delimited by
              specified delimiting string?
              >
              or do I have to roll my own/ IT is not hard to roil my own but hate to
              re-invent the wheel. I have searched built-in help. Google but failed to
              used the right search term to come anything good
              >
              >
              >
              >

              Comment

              • Marc Gravell

                #8
                Re: convert generic string list to one string

                starting with empty string

                If you do really mean "string" here, then note that this may be more
                than a little inefficient, due to the immutability of strings (so your
                loop would create *lots* of GEN0 objects, and consume telescoping
                memory (i.e. n1 + [n1+n2] + [n1+n2+n3] + [n1+n2+n3+n4] + ... if you
                see what I mean [give-or-take the separators]).
                String concatenation inside a loop is an ideal candidate for
                StringBuilder (which is supported under CF).

                Explanation: www.pobox.com/~skeet/csharp/stringbuilder.html

                Of course, if you aren't actually using "string" concatenation then it
                isn't an issue.

                Marc

                Comment

                • GS

                  #9
                  Re: convert generic string list to one string

                  thank you for the concern. That has been take care of. I am using
                  stringboard and its append method.

                  "Marc Gravell" <marc.gravell@g mail.comwrote in message
                  news:f2dcfc93-36b6-4cef-b73a-e0a2357e643f@d2 7g2000prf.googl egroups.com...
                  starting with empty string
                  >
                  If you do really mean "string" here, then note that this may be more
                  than a little inefficient, due to the immutability of strings (so your
                  loop would create *lots* of GEN0 objects, and consume telescoping
                  memory (i.e. n1 + [n1+n2] + [n1+n2+n3] + [n1+n2+n3+n4] + ... if you
                  see what I mean [give-or-take the separators]).
                  String concatenation inside a loop is an ideal candidate for
                  StringBuilder (which is supported under CF).
                  >
                  Explanation: www.pobox.com/~skeet/csharp/stringbuilder.html
                  >
                  Of course, if you aren't actually using "string" concatenation then it
                  isn't an issue.
                  >
                  Marc
                  >

                  Comment

                  • GS

                    #10
                    Re: convert generic string list to one string

                    oops, pressed the enter key instead of backspace while correcting the
                    StingBuilder typo. my apology..

                    "GS" <gsmsnews.micro soft.comGS@msne ws.Nomail.comwr ote in message
                    news:eBHW67oPIH A.4584@TK2MSFTN GP03.phx.gbl...
                    thank you for the concern. That has been take care of. I am using
                    stringboard and its append method.
                    >
                    "Marc Gravell" <marc.gravell@g mail.comwrote in message
                    news:f2dcfc93-36b6-4cef-b73a-e0a2357e643f@d2 7g2000prf.googl egroups.com...
                    starting with empty string
                    If you do really mean "string" here, then note that this may be more
                    than a little inefficient, due to the immutability of strings (so your
                    loop would create *lots* of GEN0 objects, and consume telescoping
                    memory (i.e. n1 + [n1+n2] + [n1+n2+n3] + [n1+n2+n3+n4] + ... if you
                    see what I mean [give-or-take the separators]).
                    String concatenation inside a loop is an ideal candidate for
                    StringBuilder (which is supported under CF).

                    Explanation: www.pobox.com/~skeet/csharp/stringbuilder.html

                    Of course, if you aren't actually using "string" concatenation then it
                    isn't an issue.

                    Marc
                    >
                    >

                    Comment

                    Working...