locked
How to Join an Int Array as a String Array RRS feed

  • Question

  • How to Join an Int Array as a String Array
    Ex: {0, 1, 2, 3, 4, 5}   -->  "0 1 2 3 4 5"
    Sunday, September 18, 2011 11:02 AM

Answers


  • Here is one of a number of possible solutions:
     
    using System;
    using System.IO;
     
    namespace ConsoleApplication1
    {
     class Program
     {
      static void Main(string[] args)
      {
       int[] values = new int [] { 0, 1, 2, 3, 4, 5 };
       string output = "";
     
       using (StringWriter sw = new StringWriter())
       {
        foreach (int value in values)
         sw.Write(" {0}", value);
        output = sw.ToString().Trim();
       }
     
       Console.WriteLine(output);
       Console.ReadKey();
      }
     }
    }

    --
    Mike
    Sunday, September 18, 2011 11:24 AM

All replies

  • I read this some where before. I wonder where?
    Regards David R
    ---------------------------------------------------------------
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.
    Sunday, September 18, 2011 11:16 AM

  • Here is one of a number of possible solutions:
     
    using System;
    using System.IO;
     
    namespace ConsoleApplication1
    {
     class Program
     {
      static void Main(string[] args)
      {
       int[] values = new int [] { 0, 1, 2, 3, 4, 5 };
       string output = "";
     
       using (StringWriter sw = new StringWriter())
       {
        foreach (int value in values)
         sw.Write(" {0}", value);
        output = sw.ToString().Trim();
       }
     
       Console.WriteLine(output);
       Console.ReadKey();
      }
     }
    }

    --
    Mike
    Sunday, September 18, 2011 11:24 AM
  • Thanks for the help!
    Sunday, September 18, 2011 1:20 PM