Use the PadLeft() and PadRight() methods to align strings : String : String C# Examples


C# Examples » String » String »

 

Use the PadLeft() and PadRight() methods to align strings









    
using  System;

class  MainClass
{

    public  static  void  Main()
    {
        string[]  myStrings  =  {"To",  "be",  "or",  "not",  "to",  "be"};
        string  myString  =  String.Join(".",  myStrings);      
        
        string  myString14  =  '('  +  myString.PadLeft(20)  +  ')';
        Console.WriteLine("'('  +  myString.PadLeft(20)  +  ')'  =  "  +  myString14);
        string  myString15  =  '('  +  myString.PadLeft(20,  '.')  +  ')';
        Console.WriteLine("'('  +  myString.PadLeft(20,  '.')  =  "  +  myString15);
        string  myString16  =  '('  +  myString.PadRight(20)  +  ')';
        Console.WriteLine("'('  +  myString.PadRight(20)  +  ')'  =  "  +  myString16);
        string  myString17  =  '('  +  myString.PadRight(20,  '.')  +  ')';
        Console.WriteLine("'('  +  myString.PadRight(20,  '.')  +  ')'  =  "  +  myString17);

    }

}
    
   
  
   



Output

'(' + myString.PadLeft(20) + ')' = (  To.be.or.not.to.be)
'(' + myString.PadLeft(20, '.') = (..To.be.or.not.to.be)
'(' + myString.PadRight(20) + ')' = (To.be.or.not.to.be  )
'(' + myString.PadRight(20, '.') + ')' = (To.be.or.not.to.be..)


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo String
» String