Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion from Int array to string array

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();         } 
like image 592
InfantPro'Aravind' Avatar asked Dec 27 '12 07:12

InfantPro'Aravind'


People also ask

How do I convert a number array to a string?

To convert an array of numbers to an array of strings, call the map() method on the array, and on each iteration, convert the number to a string. The map method will return a new array containing only strings.

Can we convert int array to string in Java?

toString(int[]) method returns a string representation of the contents of the specified int array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space).

Can we convert array to string in Java?

So how to convert String array to String in java. We can use Arrays. toString method that invoke the toString() method on individual elements and use StringBuilder to create String. We can also create our own method to convert String array to String if we have some specific format requirements.


2 Answers

int[] intarray = { 1, 2, 3, 4, 5 }; string[] result = intarray.Select(x=>x.ToString()).ToArray(); 
like image 54
Tilak Avatar answered Sep 28 '22 06:09

Tilak


Try Array.ConvertAll

int[] myInts = { 1, 2, 3, 4, 5 };  string[] result = Array.ConvertAll(myInts, x=>x.ToString()); 
like image 37
Rolwin Crasta Avatar answered Sep 28 '22 07:09

Rolwin Crasta