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

   }
}

Result

Quiz

Exercise