Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int array to string

Tags:

arrays

string

c#

In C#, I have an array of ints, containing digits only. I want to convert this array to string.

Array example:

int[] arr = {0,1,2,3,0,1}; 

How can I convert this to a string formatted as: "012301"?

like image 204
user397232 Avatar asked Nov 30 '09 22:11

user397232


People also ask

Can we convert int array to string?

Arrays. 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 ("[]").

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 array to string?

Convert Array to String. Sometimes we need to convert an array of strings or integers into a string, but unfortunately, there is no direct method to perform this conversion. The default implementation of the toString() method on an array returns something like Ljava. lang.


2 Answers

at.net 3.5 use:

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

at.net 4.0 or above use: (see @Jan Remunda's answer)

 string result = string.Join("", array); 
like image 84
JSBձոգչ Avatar answered Sep 23 '22 13:09

JSBձոգչ


You can simply use String.Join function, and as separator use string.Empty because it uses StringBuilder internally.

string result = string.Join(string.Empty, new []{0,1,2,3,0,1}); 

E.g.: If you use semicolon as separator, the result would be 0;1;2;3;0;1.

It actually works with null separator, and second parameter can be enumerable of any objects, like:

string result = string.Join(null, new object[]{0,1,2,3,0,"A",DateTime.Now}); 
like image 35
Jan Remunda Avatar answered Sep 21 '22 13:09

Jan Remunda