Converting a char[] to a String

T

trellow422

How do I convert the following variable to a variable of
type String?

char[] record = new char[200];

I have tried the following:
String temp = record.ToString();
temp = System.Convert.ToString(record);

The result I get with both is "System.Char[]"

Any help would be greatly appreciated.

Thanks!
 
W

William Stacey

Here is one way:

char[] ca = new char[]{'0','1'};
string str = new string(ca, 0, 2);
Console.WriteLine("MyString from ca:"+str);
 
J

Jon Skeet [C# MVP]

trellow422 said:
How do I convert the following variable to a variable of
type String?

char[] record = new char[200];

I have tried the following:
String temp = record.ToString();
temp = System.Convert.ToString(record);

The result I get with both is "System.Char[]"

Use the constructor for string which takes a char array:

string temp = new string(record);
 
J

Jan Tielens

You can create a new string based upon the char array:
string s = new string(record);

Maybe there are some other (better) solutions, but this one worked for me.
 
G

Guest

Passing the char buffer to the String constructor works!

Thank you! Thank you! Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top