-3

Possible Duplicate:
.NET / C# - Convert char[] to string

When I try to call .ToString() on my char[] I expect a string to be created of the values in the char[]. Instead I get "char[]" as a string, the type, which is not what I want. What am I missing here?

In Java .toString() on a char[] prints the values from a char[] as a string.

Thanks

Community
  • 1
  • 1
The Internet
  • 7,959
  • 10
  • 54
  • 89
  • Any code to show us what you're doing? – Lee Taylor Aug 20 '12 at 23:18
  • What is the complexity of string(arr) ? What is it doing behind the scenes? – The Internet Aug 20 '12 at 23:21
  • Why would you downvote me? How am I supposed to know there was a duplicate? It didn't show up when I was posting. Excuse me for breathing. – The Internet Aug 20 '12 at 23:26
  • 1
    @Dave: *"How am I supposed to know there was a duplicate?"* - You search before posting an answer... or, you know, consult the documentation. You have to assume that such a trivial question has been asked before, this is just lazy. – Ed S. Aug 20 '12 at 23:30
  • @EdS. I did search and this is what showed up. http://stackoverflow.com/search?q=char%5B%5D+to+string+in+c%23 - Notice that besides my post there are hundreds of results. None of the correct answers are on the first 5 pages. Do you want me to waste more time? That's not lazy, that's efficient. – The Internet Aug 20 '12 at 23:33
  • And when you add the word "convert"... http://stackoverflow.com/search?q=convert+char%5B%5D+to+string+in+c%23&submit=search – Ed S. Aug 20 '12 at 23:34
  • 1
    Also, checking the documentation for `String` would have been a good first stab. – Ed S. Aug 20 '12 at 23:34
  • The word convert wasn't in my mind. I'm sorry I don't have the .NET API memorized. I hope you feel good about yourself. – The Internet Aug 20 '12 at 23:38
  • @exacerbatedexpert I don't appreciate your sarcasm. I understand marking a question as closed because it is a duplicate, but down-voting is a totally different thing. That is punishing ignorance. You had to make the mental assumption that I am lazy and didn't try to search for an answer which was completely false. I did try to search. Just because I couldn't think of the word "convert" and come to the right duplicate answer does not warrant a downvote. I'm sorry. And I know the SO founders would agree. – The Internet Aug 20 '12 at 23:49

4 Answers4

5

Good old String constructor.

http://msdn.microsoft.com/en-us/library/ttyxaek9.aspx

new String(chars)
dana
  • 17,267
  • 6
  • 64
  • 88
  • 2
    +1. to all calls to constructor. Sort of sad to see question "how to create string from XXX" that is directly listed on MSDN... – Alexei Levenkov Aug 20 '12 at 23:21
5
char[] charArray = new char[10];
.... 
string myString = new string(charArray);
Martin
  • 6,632
  • 4
  • 25
  • 28
4

As simple as:

string s = new string(arr);
zerkms
  • 249,484
  • 69
  • 436
  • 539
1

That's quite simple actually:

char[] myCharArray = new char[5];

myCharArray[0] = 'H';
myCharArray[1] = 'e';
myCharArray[2] = 'l';
myCharArray[3] = 'l';
myCharArray[4] = 'o';

string myString = new String(myCharArray);
Console.WriteLine("This is my String: " + myString;

Right? :-)

Yaroze
  • 131
  • 1
  • 3
  • 10