How to add specified number of space in the string?

Posted by SheoNarayan under C# category on | Views : 21064
There might be situation when you need to add specified number of character in the left or right of the string.

For example:

you have a string variable called command with value as "123" and you want to add 10 ' ' (blank space) in the left of this string so that it becomes " 123", you can use PadLeft method of the string.

string str = "123";
str = str.PadLeft(10, ' ');

In the same way, if you want to add the blank space at the right of the string, you can use PadRight method, the parameters are same as PadLeft method. You can specify any character as 2nd parameter in the above method.

Comments or Responses

Login to post response