Generic List

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

    #1

    Generic List

    Hello,

    How can I convert a Generic.List(Of String) to a single string where
    all elements are separated by a comma?

    My string would become:

    "item1,item2,it em3"

    Thanks,

    Miguel

  • Grant Merwitz

    #2
    Re: Generic List

    C# example

    List<stringStrl ist = new List<string>();

    StrList.Add("1" );
    StrList.Add("2" );
    StrList.Add("3" );

    string SingleString = "";
    foreach(string s in Strlist.ToArray ())
    SingleString += s + ",";

    You can then either remove the last ","
    or in the loop work out which is the first item or last

    "shapper" <mdmoura@gmail. comwrote in message
    news:1175001163 .183302.89390@o 5g2000hsb.googl egroups.com...
    Hello,
    >
    How can I convert a Generic.List(Of String) to a single string where
    all elements are separated by a comma?
    >
    My string would become:
    >
    "item1,item2,it em3"
    >
    Thanks,
    >
    Miguel
    >

    Comment

    • =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=

      #3
      Re: Generic List

      Howdy,

      I'd would not recommend this approach as is inefficient (first copy to
      array, and then string concatenation). This is much faster:

      public static string ConcatList<T>(
      System.Collecti ons.Generic.ILi st<Tlist, char separator)
      {
      System.Text.Str ingBuilder text =
      new System.Text.Str ingBuilder();

      for (int i = 0; i < list.Count; i++)
      {
      text.Append(lis t[i]);
      text.Append(sep arator);
      }

      if (text.Length 0)
      text.Length -= 1;

      return text.ToString() ;
      }
      --
      Milosz


      "Grant Merwitz" wrote:
      C# example
      >
      List<stringStrl ist = new List<string>();
      >
      StrList.Add("1" );
      StrList.Add("2" );
      StrList.Add("3" );
      >
      string SingleString = "";
      foreach(string s in Strlist.ToArray ())
      SingleString += s + ",";
      >
      You can then either remove the last ","
      or in the loop work out which is the first item or last
      >
      "shapper" <mdmoura@gmail. comwrote in message
      news:1175001163 .183302.89390@o 5g2000hsb.googl egroups.com...
      Hello,

      How can I convert a Generic.List(Of String) to a single string where
      all elements are separated by a comma?

      My string would become:

      "item1,item2,it em3"

      Thanks,

      Miguel
      >
      >
      >

      Comment

      • Grant Merwitz

        #4
        Re: Generic List

        thanks for a more efficient method.

        I'll be sure to use that in future

        "Milosz Skalecki [MCAD]" <mily242@DONTLI KESPAMwp.plwrot e in message
        news:ED93287E-B641-4954-A562-344CB96FC111@mi crosoft.com...
        Howdy,
        >
        I'd would not recommend this approach as is inefficient (first copy to
        array, and then string concatenation). This is much faster:
        >
        public static string ConcatList<T>(
        System.Collecti ons.Generic.ILi st<Tlist, char separator)
        {
        System.Text.Str ingBuilder text =
        new System.Text.Str ingBuilder();
        >
        for (int i = 0; i < list.Count; i++)
        {
        text.Append(lis t[i]);
        text.Append(sep arator);
        }
        >
        if (text.Length 0)
        text.Length -= 1;
        >
        return text.ToString() ;
        }
        --
        Milosz
        >
        >
        "Grant Merwitz" wrote:
        >
        >C# example
        >>
        >List<stringStr list = new List<string>();
        >>
        >StrList.Add("1 ");
        >StrList.Add("2 ");
        >StrList.Add("3 ");
        >>
        >string SingleString = "";
        >foreach(stri ng s in Strlist.ToArray ())
        > SingleString += s + ",";
        >>
        >You can then either remove the last ","
        >or in the loop work out which is the first item or last
        >>
        >"shapper" <mdmoura@gmail. comwrote in message
        >news:117500116 3.183302.89390@ o5g2000hsb.goog legroups.com...
        Hello,
        >
        How can I convert a Generic.List(Of String) to a single string where
        all elements are separated by a comma?
        >
        My string would become:
        >
        "item1,item2,it em3"
        >
        Thanks,
        >
        Miguel
        >
        >>
        >>
        >>

        Comment

        Working...