Conversion from Int array to string array
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();
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, 2021Comments
-
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[]
tostring
(notstring[]
). 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(); }