Mega Code Archive

 
Categories / C# / Data Types
 

Pad String

/*  * Copyright (c) United Binary LLC.  All rights reserved.  *   * This code is licensed under the MIT License  *   * SEE: http://harnessit.codeplex.com/license  *   */ #region using ... using System; using System.Text; #endregion namespace UnitedBinary.Core.Utility.Text {   /// <include file='Text.xml' path='/Docs/Format/Class/*'/>   public sealed class Format   {     private Format() {}     /// <include file='Text.xml' path='/Docs/Format/PadString/*'/>     public static string PadString(string text, char padChar, int length, bool padFromFront)     {       if (length <= 0)       {         throw new ArgumentException("Length must be positive.", "length");       }       if (text == null)       {         text = string.Empty;       }       StringBuilder sb = new StringBuilder(length);       if (padFromFront)       {         int currentLength = 0;         int padsNeeded = length - text.Length;         while (currentLength < padsNeeded)         {           currentLength++;           sb.Append(padChar);         }         sb.Append(text);       }       else       {         sb.Append(text);         int currentLength = text.Length;         while (currentLength < length)         {           currentLength++;           sb.Append(padChar);         }       }       string paddedString = sb.ToString();       return paddedString;     }   } }