506

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466

8 Answers8

830

There's a constructor for this:

char[] chars = {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'};
string s = new string(chars);
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 20
    Note that `new string(null)` yields `String.Empty` and **not** `null`! If you want to keep `null`, you can make an extension method `static string ToStringSafe(this char[] buf) { return buf == null ? null : new string(buf); }`. – Skod Aug 10 '15 at 20:31
  • 17
    @Skod: Seeing that it's impossible for a "new" expression to return a value other than an object instance, that should not come as a surprise to anyone. – Matti Virkkunen Jan 12 '18 at 16:53
  • 3
    @MattiVirkkunen: Throwing an exception is also a reasonable thing to do. That's the behavior I would have guessed for passing null to the string constructor. – Charles Taylor Jul 28 '18 at 17:33
  • 1
    Valid for `Path.GetInvalidFileNameChars()` ? – Kiquenet Oct 04 '22 at 13:54
105

Use the constructor of string which accepts a char[]

char[] c = ...;
string s = new string(c);
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 70
    If only you were three minutes faster, you would have answered before the question was asked! – DCastenholz Mar 30 '14 at 22:48
  • 8
    Forget minutes. It's just 17 seconds. My answer just above is my 2nd high-est voted answer on the site. In fact I'm here now because someone just voted it again, almost 10 years later. And the two answers aren't really any different... but mine was posted 17 seconds faster, and that's meant a 500 vote difference :/ – Joel Coehoorn Mar 14 '19 at 16:26
43
char[] characters;
...
string s = new string(characters);
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
34

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"
Phill
  • 18,398
  • 7
  • 62
  • 102
Simon Achmüller
  • 1,096
  • 13
  • 26
26

Another alternative

char[] c = { 'R', 'o', 'c', 'k', '-', '&', '-', 'R', 'o', 'l', 'l' };
string s = String.Concat( c );

Debug.Assert( s.Equals( "Rock-&-Roll" ) );
Michael J
  • 2,443
  • 2
  • 23
  • 24
  • 3
    `String.Concat` is nice because it accepts `IEnumerable` as a parameter, so we don't have to call `ToArray()` when using LINQ. – Fábio Batista Sep 04 '22 at 00:16
23
String mystring = new String(mychararray);
Bali C
  • 30,582
  • 35
  • 123
  • 152
Shaun Rowland
  • 411
  • 3
  • 5
22

Use the string constructor which accepts chararray as argument, start position and length of array. Syntax is given below:

string charToString = new string(CharArray, 0, CharArray.Count());
Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118
Dilip Nannaware
  • 1,410
  • 1
  • 16
  • 24
  • I think that's what I was looking for. But I didn't know of any class named CharArray. Did you perhaps mean something like: `char[] charArray = new char[5] { 'a', 'b', 'c', '\0', '\0' }; string charsAsString = new string(charArray, 0, 3); // only want part of array.` – Suncat2000 Apr 17 '19 at 19:08
  • CharArray is not class, just variable of type char[] – Dilip Nannaware Apr 17 '19 at 19:16
0

String.Join(string separator, char[] charArray) concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member:

char[] c = { 'A', ' ', 'B', ' ', 'C', '&', 'D'};
string stringResult =  string.Join("", c);  //A B C&D
Hossein Sabziani
  • 1
  • 2
  • 15
  • 20