943,685 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 4314
  • C# RSS
Aug 12th, 2008
0

How to append string in front

Expand Post »
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"
Similar Threads
Reputation Points: 12
Solved Threads: 2
Newbie Poster
raj416 is offline Offline
14 posts
since Jan 2008
Aug 12th, 2008
0

Re: How to append string in front

Last edited by Jx_Man; Aug 12th, 2008 at 4:12 am.
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,138 posts
since Nov 2007
Aug 12th, 2008
0

Re: How to append string in front

Or you could just go the easy route

C# Syntax (Toggle Plain Text)
  1. string s= "sample";
  2. s = "daniweb " + s;
Last edited by dickersonka; Aug 12th, 2008 at 12:37 pm.
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Aug 12th, 2008
0

Re: How to append string in front

Or you could just go the easy route

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 4
Light Poster
nvmobius is offline Offline
39 posts
since Jul 2008
Aug 12th, 2008
0

Re: How to append string in front

Correct nvmobius, I'll add if you are doing this with more than just a simple example like that, might want to try this.

C# Syntax (Toggle Plain Text)
  1. StringBuilder sb = new StringBuilder();
  2. sb.Append("sample");
  3. sb.Insert(0, "daniweb ");
  4. string s = sb.ToString();
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Aug 12th, 2008
0

Re: How to append string in front

Correct nvmobius, I'll add if you are doing this with more than just a simple example like that, might want to try this.

C# Syntax (Toggle Plain Text)
  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).
Reputation Points: 11
Solved Threads: 4
Light Poster
nvmobius is offline Offline
39 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Sort/Filter Data in generic collection
Next Thread in C# Forum Timeline: Forms App Behaving Oddly Based On User





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC