529

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.

0

8 Answers 8

866

There's a constructor for this:

char[] chars = {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'};
string s = new string(chars);
4
  • 21
    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
    Commented Aug 10, 2015 at 20:31
  • 18
    @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. Commented Jan 12, 2018 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. Commented Jul 28, 2018 at 17:33
  • 1
    Valid for Path.GetInvalidFileNameChars() ?
    – Kiquenet
    Commented Oct 4, 2022 at 13:54
114

Use the constructor of string which accepts a char[]

char[] c = ...;
string s = new string(c);
3
  • 71
    If only you were three minutes faster, you would have answered before the question was asked! Commented Mar 30, 2014 at 22:48
  • 10
    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 :/ Commented Mar 14, 2019 at 16:26
  • @JoelCoehoorn - 14 years later, and another vote! Cheers.
    – Neil
    Commented Oct 24, 2023 at 11:42
44
char[] characters;
...
string s = new string(characters);
35

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"
4
  • string.Join only accepts string[] instead of char[]
    – sky91
    Commented Aug 2, 2017 at 8:16
  • @sky91 not only, you can use String.Join<T> Method and any T[] as parameter, T.ToString() will be called Commented Aug 3, 2017 at 8:19
  • 1
    then you should write string.Join<char>("_", chars) instead of string.Join("_", chars)
    – sky91
    Commented Aug 3, 2017 at 8:42
  • 4
    Compiler does it automatically as chars type is known on compile time. Commented Aug 3, 2017 at 15:15
30

Another alternative

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

Debug.Assert( s.Equals( "Rock-&-Roll" ) );
1
  • 4
    String.Concat is nice because it accepts IEnumerable<char> as a parameter, so we don't have to call ToArray() when using LINQ. Commented Sep 4, 2022 at 0:16
23
String mystring = new String(mychararray);
22

Use the string constructor which takes as arguments a Char array, the start position in the array, and the length of array. Syntax is given below:

string stringFromArray = new string(charArray, 0, charArray.Length);
2
  • 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
    Commented Apr 17, 2019 at 19:08
  • 1
    CharArray is not class, just variable of type char[] Commented Apr 17, 2019 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.