andrew4582

PadTitle

Oct 4th, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class MyClass
  5. {
  6.     public static void Main()
  7.     {
  8.         Console.WriteLine(PadTitle("Hello",60));
  9.         Console.WriteLine(PadTitle("",60));
  10.         Console.WriteLine(PadTitle("World",60,'*'));
  11.        
  12.         Console.Write("Press any key to continue...");
  13.         Console.ReadKey();
  14.     }
  15.    
  16.     static string PadTitle(string s, int totalLength)
  17.     {
  18.         char paddchar = '-';
  19.         return PadTitle(s,totalLength,paddchar);
  20.     }
  21.    
  22.     static string PadTitle(string s, int totalLength,char paddchar)
  23.     {
  24.         int padlen = (totalLength - s.Length) / 2;
  25.         int leftPad = padlen;
  26.         int rightPad = rightPad = totalLength - (s.Length + leftPad);
  27.  
  28.         if(leftPad < 0 || rightPad < 0)
  29.             return s;
  30.        
  31.         string msg = "".PadLeft(leftPad,paddchar);
  32.         msg += s;
  33.         msg += "".PadRight(rightPad, paddchar);
  34.         return msg;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment