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:01 AM

Answers

  • The example you show turns an int array into a string, not a string array. To do that something like this would work:

    string s = intArray[0];
    for (int i = 1; i < intArray.Length; i++)
    {
       s += " " + intArray[i];
    }

    To turn it into a string array:

    int[] intArray = new [] {0, 1 ,2 ,3 ,4, 5};
    string[] s = new string[intArray.Length];
    for (int i = 0; i < s.Length; i++)
    {
       s[i] = intArray[i].ToString();
    }

     


    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:15 AM
  • Try to "convert" from int[] to string[] :

     

    string[] strArray = Array.ConvertAll(intArray, n => n.ToString());
    



    Mitja
    Sunday, September 18, 2011 12:46 PM
  • Hi Mitja,

    You're right. To get the expected "0 1 2 3 4 5", you also will have to join the string array to a string:

    int[] intArray = new[] { 1, 2, 3, 4, 5 };
    string[] strArray = Array.ConvertAll(intArray, i => i.ToString());
    string numberString = String.Join(" ", strArray);
    


    Marcel

    Sunday, September 18, 2011 1:17 PM

All replies

  • The example you show turns an int array into a string, not a string array. To do that something like this would work:

    string s = intArray[0];
    for (int i = 1; i < intArray.Length; i++)
    {
       s += " " + intArray[i];
    }

    To turn it into a string array:

    int[] intArray = new [] {0, 1 ,2 ,3 ,4, 5};
    string[] s = new string[intArray.Length];
    for (int i = 0; i < s.Length; i++)
    {
       s[i] = intArray[i].ToString();
    }

     


    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:15 AM
  • Sorry, I saw this post first in the "C# Language" forum, and answered a slightly different approach using stringwriter.

    --
    Mike
    Sunday, September 18, 2011 11:33 AM
  • Try to "convert" from int[] to string[] :

     

    string[] strArray = Array.ConvertAll(intArray, n => n.ToString());
    



    Mitja
    Sunday, September 18, 2011 12:46 PM
  • Hi Mitja,

    You're right. To get the expected "0 1 2 3 4 5", you also will have to join the string array to a string:

    int[] intArray = new[] { 1, 2, 3, 4, 5 };
    string[] strArray = Array.ConvertAll(intArray, i => i.ToString());
    string numberString = String.Join(" ", strArray);
    


    Marcel

    Sunday, September 18, 2011 1:17 PM
  • Thanks for the help!
    Sunday, September 18, 2011 1:23 PM