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"

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.

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();

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 article has been dead for over six months. Start a new discussion instead.