Conversin between chars and strings

A

Alfons

Hi,

How can I convert an array of chars into a string and vice
versa ?

char[] myChars = new char[10];

I want to convert these 10 chars into a single string, but
how ?

And if I have a single string, like

string ss = "abcdef";

How can I make an array of chars from this ?

Thx,
Alfons
 
J

Joakim Karlsson

Alfons said:
Hi,

How can I convert an array of chars into a string and vice
versa ?

char[] myChars = new char[10];

I want to convert these 10 chars into a single string, but
how ?

String str = new String(myChars);
And if I have a single string, like

string ss = "abcdef";

How can I make an array of chars from this ?

char[] myChars = new char[ss.Length];
str.CopyTo((0, myChars , 0, str.Length);

/Joakim
 
J

Joerg Jooss

Alfons said:
Hi,

How can I convert an array of chars into a string and vice
versa ?

char[] myChars = new char[10];

I want to convert these 10 chars into a single string, but
how ?

string str = new string(mychars);

And if I have a single string, like

string ss = "abcdef";

How can I make an array of chars from this ?

char[] chars = ss.ToCharArray();


It's all on MSDN...
 
J

Joakim Karlsson

Joakim said:
char[] myChars = new char[ss.Length];
str.CopyTo((0, myChars , 0, str.Length);

Doh! Me stoopid! :)

Of course you should use:
char[] myChars = str.ToCharArray();

Bad Joakim! Bad!
 

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