538

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

884

There's a constructor for this:

char[] chars = {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'}; string s = new string(chars); 
Sign up to request clarification or add additional context in comments.

4 Comments

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: 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.
@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.
Valid for Path.GetInvalidFileNameChars() ?
117

Use the constructor of string which accepts a char[]

char[] c = ...; string s = new string(c); 

3 Comments

If only you were three minutes faster, you would have answered before the question was asked!
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 :/
@JoelCoehoorn - 14 years later, and another vote! Cheers.
44
char[] characters; ... string s = new string(characters); 

Comments

37

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 Comments

string.Join only accepts string[] instead of char[]
@sky91 not only, you can use String.Join<T> Method and any T[] as parameter, T.ToString() will be called
then you should write string.Join<char>("_", chars) instead of string.Join("_", chars)
Compiler does it automatically as chars type is known on compile time.
37

Another alternative

char[] c = { 'R', 'o', 'c', 'k', '-', '&', '-', 'R', 'o', 'l', 'l' }; string s = String.Concat( c ); Debug.Assert( s.Equals( "Rock-&-Roll" ) ); 

1 Comment

String.Concat is nice because it accepts IEnumerable<char> as a parameter, so we don't have to call ToArray() when using LINQ.
23
String mystring = new String(mychararray); 

Comments

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 Comments

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.
CharArray is not class, just variable of type char[]
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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.