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 .  j  ava 2  s.c  om
{
   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   ww w .j  av a  2 s.c  o m
{
   public static void Main(string[] args)
   {
         Console.WriteLine ("12345".PadLeft (2, '*')); 
         Console.WriteLine ("12345".PadLeft (2));      

   }
}

Result

Quiz

Exercise