Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ARRAY LIST TO STRING

ARRAY LIST TO STRING

Scheduled Pinned Locked Moved C#
data-structurestutorial
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lawrenceinba
    wrote on last edited by
    #1

    how to convert array list into single string seperated with spaces

    the quieter u become more u hear

    K M A 3 Replies Last reply
    0
    • L lawrenceinba

      how to convert array list into single string seperated with spaces

      the quieter u become more u hear

      K Offline
      K Offline
      Kristian Sixhoj
      wrote on last edited by
      #2

      You'd wanna look at the String.Concat[^] method.

      :bob: Kristian Sixhoej "You can always become better." - Tiger Woods

      1 Reply Last reply
      0
      • L lawrenceinba

        how to convert array list into single string seperated with spaces

        the quieter u become more u hear

        M Offline
        M Offline
        musefan
        wrote on last edited by
        #3

        string result = ""; foreach(object item in ArrayList) { result += item.ToString() + " "; } result = result.TrimEnd(new string[]{" "}); that should do it

        If only MySelf.Visible was more than just a getter...

        D 1 Reply Last reply
        0
        • L lawrenceinba

          how to convert array list into single string seperated with spaces

          the quieter u become more u hear

          A Offline
          A Offline
          ABitSmart
          wrote on last edited by
          #4

          Try this,

          ArrayList temp = new ArrayList();
          temp.Add("A");
          temp.Add("B");

          string delim = " "; //space
          string result = string.Join(delim, (string[])temp.ToArray(typeof(string)));

          //result will be "A B"

          1 Reply Last reply
          0
          • M musefan

            string result = ""; foreach(object item in ArrayList) { result += item.ToString() + " "; } result = result.TrimEnd(new string[]{" "}); that should do it

            If only MySelf.Visible was more than just a getter...

            D Offline
            D Offline
            Deresen
            wrote on last edited by
            #5

            You can better use the StringBuilder for these kind of operations, they are faster:

            StringBuilder result = new StringBuilder();
            foreach(object item in ArrayList)
            {
            result.AppendLine(item.ToString());
            }
            String result2 = result.ToString();

            Now you have them seperated by a linebreak. You can also just use the append like this to separate by spaces:

            StringBuilder result = new StringBuilder();
            foreach(object item in ArrayList)
            {
            result.Append(item.ToString() + " ");
            }
            String result2 = result.ToString();
            result2 = result2.Remove(result2.Length-1);

            L L 2 Replies Last reply
            0
            • D Deresen

              You can better use the StringBuilder for these kind of operations, they are faster:

              StringBuilder result = new StringBuilder();
              foreach(object item in ArrayList)
              {
              result.AppendLine(item.ToString());
              }
              String result2 = result.ToString();

              Now you have them seperated by a linebreak. You can also just use the append like this to separate by spaces:

              StringBuilder result = new StringBuilder();
              foreach(object item in ArrayList)
              {
              result.Append(item.ToString() + " ");
              }
              String result2 = result.ToString();
              result2 = result2.Remove(result2.Length-1);

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Deresen wrote:

              result.Append(item.ToString() + " ");

              when result is a StringBuilder you should not use string concatenation at all! Instead do:

              result.Append(item.ToString());
              result.Append(" ");

              :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              modified on Friday, June 10, 2011 11:43 PM

              1 Reply Last reply
              0
              • D Deresen

                You can better use the StringBuilder for these kind of operations, they are faster:

                StringBuilder result = new StringBuilder();
                foreach(object item in ArrayList)
                {
                result.AppendLine(item.ToString());
                }
                String result2 = result.ToString();

                Now you have them seperated by a linebreak. You can also just use the append like this to separate by spaces:

                StringBuilder result = new StringBuilder();
                foreach(object item in ArrayList)
                {
                result.Append(item.ToString() + " ");
                }
                String result2 = result.ToString();
                result2 = result2.Remove(result2.Length-1);

                L Offline
                L Offline
                lawrenceinba
                wrote on last edited by
                #7

                thanks a lot its working fine

                the quieter u become more u hear

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups