CSharp - String String padding

Introduction

PadLeft and PadRight pad a string to a given length with a specified character or a space if unspecified.

Demo

using System;
class MainClass//from   w w w  .  ja  v  a 2s  .co m
{
   public static void Main(string[] args)
   {
         Console.WriteLine ("12345".PadLeft (9, '*')); 
         Console.WriteLine ("12345".PadLeft (9));      

   }
}

Result

If the input string is longer than the padding length, the original string is returned unchanged.

Demo

using System;
class MainClass/*from  w w w  .  j  av a 2  s.  co m*/
{
   public static void Main(string[] args)
   {
         Console.WriteLine ("12345".PadLeft (2, '*')); 
         Console.WriteLine ("12345".PadLeft (2));      

   }
}

Result

Quiz

Exercise