Pad the left side of a string with characters to make the total length. - CSharp System

CSharp examples for System:Char

Description

Pad the left side of a string with characters to make the total length.

Demo Code

/*//from  w ww .  ja  v a 2  s. com
 * @version   : 2.5.0
 * @author    : Ext.NET, Inc. http://www.ext.net/
 * @date      : 2014-10-20
 * @copyright : Copyright (c) 2008-2014, Ext.NET, Inc. (http://www.ext.net/). All rights reserved.
 * @license   : See license.txt and http://www.ext.net/license/. 
 * @website   : http://www.ext.net/
 */
using System.Web.UI;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;
using System;

public class Main{
        /// <summary>
        /// Pad the left side of a string with characters to make the total length.
        /// </summary>
        public static string PadLeft(this string text, char c, Int32 totalLength)
        {
            if (text.IsEmpty())
            {
                return text;
            }

            if (totalLength < text.Length)
            {
                return text;
            }

            return new String(c, totalLength - text.Length) + text;
        }
        /// <summary>
        /// Determine is the string is null or empty.
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool IsEmpty(this string text)
        {
            return string.IsNullOrEmpty(text);
        }
}

Related Tutorials