Conversion from Int array to string array

62,419

Solution 1

int[] intarray = { 1, 2, 3, 4, 5 };
string[] result = intarray.Select(x=>x.ToString()).ToArray();

Solution 2

Try Array.ConvertAll

int[] myInts = { 1, 2, 3, 4, 5 };

string[] result = Array.ConvertAll(myInts, x=>x.ToString());

Solution 3

Here you go:

Linq version:

String.Join(",", new List<int>(array).ConvertAll(i => i.ToString()).ToArray());

Simple one:

string[] stringArray = intArray.Select(i => i.ToString()).ToArray();
Share:
62,419
InfantPro'Aravind'
Author by

InfantPro'Aravind'

An Infant Pro 'Aravind' Siebel, HTML, CSS, JavaScript, DHTML, XML, XPath, XSD, XSLT, VBScript, VB, C#, Core Java, RegEx, SQL and so on.. interest lies in: Languages, culture, Anime-Manga, music-melody and art :) Hobbies: Singing and Pencil sketching/shade art.. One of them is already demonstrated in my profile picture :)

Updated on December 03, 2021

Comments

  • InfantPro'Aravind'
    InfantPro'Aravind' over 2 years

    When I am converting array of integers to array of string, I am doing it in a lengthier way using a for loop, like mentioned in sample code below. Is there a shorthand for this?

    The existing question and answers in SO are about int[] to string (not string[]). So they weren't helpful.

    While I found this Converting an int array to a String array answer but the platform is Java not C#. Same method can't be implemented!

            int[] intarray =  { 198, 200, 354, 14, 540 };
            Array.Sort(intarray);
            string[] stringarray = { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty};
    
            for (int i = 0; i < intarray.Length; i++)
            {
                stringarray[i] = intarray[i].ToString();
            }