.NET / C# - Convert char[] to string
419,604
Solution 1
There's a constructor for this:
char[] chars = {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'};
string s = new string(chars);
Solution 2
Use the constructor of string which accepts a char[]
char[] c = ...;
string s = new string(c);
Solution 3
char[] characters;
...
string s = new string(characters);
Solution 4
One other way:
char[] chars = {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'};
string s = string.Join("", chars);
//we get "a string"
// or for fun:
string s = string.Join("_", chars);
//we get "a_ _s_t_r_i_n_g"
Solution 5
String mystring = new String(mychararray);
Author by
BuddyJoe
I like to code C# and work with the web. Still learning.
Updated on July 08, 2022Comments
-
BuddyJoe almost 2 years
What is the proper way to turn a
char[]
into a string?The
ToString()
method from an array of characters doesn't do the trick.