What is the best way to copy char[] into a string?

  • Thread starter Thread starter A.M-SG
  • Start date Start date
A

A.M-SG

Hi,

What is the best way to copy char[] into a string?
I am trying to avoid using a loop.

Thank you,
Alan
 
char[] chars = new char[] {'1', '2', '3'.'4'}
string s = new String(chars);

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
Salam

If u ask me , i would rather use the following approach

char[] a={'a','b','c'};


StringBuilder s=new StringBuilder();

s=s.Append(a);



but , inlcude the System.Test Namespace


--
ALI RAZA SHAIKH
MCAD.net


www.programmersparadise.cjb.net
alirazashaikh.blogspot.com
 
If you need a string based only on the chars you have, then use the string
constructor. If you need a string based on those chars but need to modify it
first, consider the StringBuilder.

Of course, if you can get away with not creating either then perf would
benefit. But we'd need to know more about your exact needs.

HTH,
Kent

ALI RAZA said:
Salam

If u ask me , i would rather use the following approach

char[] a={'a','b','c'};


StringBuilder s=new StringBuilder();

s=s.Append(a);



but , inlcude the System.Test Namespace


--
ALI RAZA SHAIKH
MCAD.net


www.programmersparadise.cjb.net
alirazashaikh.blogspot.com


A.M-SG said:
Hi,

What is the best way to copy char[] into a string?
I am trying to avoid using a loop.

Thank you,
Alan
 
Hi Alan,

Agree with Kent, since the .NET's string class is immutable. If you want to
construct a string from char array , and also need to modify it, you should
consider StringBuilder. Otherwise, use the string type and its constructor
is enough.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Kent Boogaart" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: What is the best way to copy char[] into a string?
| Date: Wed, 2 Nov 2005 11:02:10 +1030
| Lines: 51
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Response
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: ppp142-169.lns2.adl2.internode.on.net 59.167.142.169
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.languages.csharp:133294
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| If you need a string based only on the chars you have, then use the
string
| constructor. If you need a string based on those chars but need to modify
it
| first, consider the StringBuilder.
|
| Of course, if you can get away with not creating either then perf would
| benefit. But we'd need to know more about your exact needs.
|
| HTH,
| Kent
|
| | > Salam
| >
| > If u ask me , i would rather use the following approach
| >
| > char[] a={'a','b','c'};
| >
| >
| > StringBuilder s=new StringBuilder();
| >
| > s=s.Append(a);
| >
| >
| >
| > but , inlcude the System.Test Namespace
| >
| >
| > --
| > ALI RAZA SHAIKH
| > MCAD.net
| >
| >
| > www.programmersparadise.cjb.net
| > alirazashaikh.blogspot.com
| >
| >
| > | >> Hi,
| >>
| >> What is the best way to copy char[] into a string?
| >> I am trying to avoid using a loop.
| >>
| >> Thank you,
| >> Alan
| >>
| >
| >
|
|
|
 
Back
Top