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"

Recommended Answers

All 5 Replies

Or you could just go the easy route

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

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.