-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

9
  • Any code to show us what you're doing? Commented Aug 20, 2012 at 23:18
  • What is the complexity of string(arr) ? What is it doing behind the scenes? Commented Aug 20, 2012 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. Commented Aug 20, 2012 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. Commented Aug 20, 2012 at 23:30
  • 1
    Also, checking the documentation for String would have been a good first stab. Commented Aug 20, 2012 at 23:34

4 Answers 4

5

Good old String constructor.

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

new String(chars) 
Sign up to request clarification or add additional context in comments.

1 Comment

+1. to all calls to constructor. Sort of sad to see question "how to create string from XXX" that is directly listed on MSDN...
5
char[] charArray = new char[10]; .... string myString = new string(charArray); 

Comments

4

As simple as:

string s = new string(arr); 

Comments

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? :-)

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.