converting an array to a string


G

Guest

Hi,

How do I convert an array to a string ?
What I did was convert a string to an array, after some manipulation, I want
to pass the value back to a string.

char[] setchecked;
setchecked = strchecked.ToCharArray();
/////
I want to convert array setchecked[] back to a string n pass the value to
string strchecked.
or should i just:

strchecked = "";
foreach (char c in setchecked)
{ strchecked += c; }


TIA.
Andrew.
 
Ad

Advertisements

K

Karl Seguin

in VB.Net you can just cast an array of characters to a string

dim strChecked as string = cstr(setChecked);

In C#, you use the .Net method (though you could do that in VB.Net also),
which is rather plainly:

string strChecked = new string(setchecked);

doh! ;)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
B

Ben

Andrew,

In .Net strings are immutable, meaning they can't be changed. This means
that each iteration in your loop allocates new memory :( If you were to
take an approach like that I'd suggest you look into
System.Text.StringBuilder. That being said, check out the System.String
constructors, specifically System.String(Char[]).

So to answer your question try
strChecked = new String(setChecked);

String
http://msdn.microsoft.com/library/d...us/cpref/html/frlrfsystemstringclasstopic.asp

StringBuilder
http://msdn.microsoft.com/library/d...ml/frlrfsystemtextstringbuilderclasstopic.asp

HTH,
Ben
 
Ad

Advertisements

G

Guest

string myString = string.Concat(setchecked);

or if you wanted a separator between each char instance use

string.Join and pass it your separator and array to join
 

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