How to append string in front

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 14
Reputation: raj416 is an unknown quantity at this point 
Solved Threads: 2
raj416 raj416 is offline Offline
Newbie Poster

How to append string in front

 
0
  #1
Aug 12th, 2008
I have a string variable .. i need to append some value in front of this string ... i tried pad left function.. but it was not working

for example

string s= "sample"


now in want to insert "daniweb " , so my output of string s will be "daniweb sample"
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,641
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: How to append string in front

 
0
  #2
Aug 12th, 2008
Last edited by Jx_Man; Aug 12th, 2008 at 4:12 am.
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: How to append string in front

 
0
  #3
Aug 12th, 2008
Or you could just go the easy route

  1. string s= "sample";
  2. s = "daniweb " + s;
Last edited by dickersonka; Aug 12th, 2008 at 12:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 39
Reputation: nvmobius is an unknown quantity at this point 
Solved Threads: 4
nvmobius nvmobius is offline Offline
Light Poster

Re: How to append string in front

 
0
  #4
Aug 12th, 2008
Originally Posted by dickersonka View Post
Or you could just go the easy route

  1. string s= "sample";
  2. s = "daniweb " + s;
This is better, unless you do it often. Remeber each + used allocates and floats another copy of the string.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: How to append string in front

 
0
  #5
Aug 12th, 2008
Correct nvmobius, I'll add if you are doing this with more than just a simple example like that, might want to try this.

  1. StringBuilder sb = new StringBuilder();
  2. sb.Append("sample");
  3. sb.Insert(0, "daniweb ");
  4. string s = sb.ToString();
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 39
Reputation: nvmobius is an unknown quantity at this point 
Solved Threads: 4
nvmobius nvmobius is offline Offline
Light Poster

Re: How to append string in front

 
0
  #6
Aug 12th, 2008
Originally Posted by dickersonka View Post
Correct nvmobius, I'll add if you are doing this with more than just a simple example like that, might want to try this.

  1. StringBuilder sb = new StringBuilder();
  2. sb.Append("sample");
  3. sb.Insert(0, "daniweb ");
  4. string s = sb.ToString();
The downside of StringBuilder (and secretly String.Format) is that the allocation/release cost is several times that of the base String class. I'll see if I can find the MS developers' post, but if I remember correctly appending 3 strings together is cheaper than allocating, and using a StringBuilder to do the same thing. 4 strings is where StringBuilder becomes cheaper (total clock cycles, including garbage collector time).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC