0

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"

4
Contributors
5
Replies
7
Views
8 Years
Discussion Span
Last Post by nvmobius
0

Or you could just go the easy route

string s= "sample";
s = "daniweb " + s;

This is better, unless you do it often. Remeber each + used allocates and floats another copy of the string.

0

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

StringBuilder sb = new StringBuilder();
  sb.Append("sample");
  sb.Insert(0, "daniweb ");
  string s = sb.ToString();
0

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

StringBuilder sb = new StringBuilder();
  sb.Append("sample");
  sb.Insert(0, "daniweb ");
  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).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.