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(); }
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.
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).
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.
int[] intarray = { 1, 2, 3, 4, 5 }; string[] result = intarray.Select(x=>x.ToString()).ToArray();
Try Array.ConvertAll
int[] myInts = { 1, 2, 3, 4, 5 }; string[] result = Array.ConvertAll(myInts, x=>x.ToString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With