Returns the last four characters of the string; if null, returns empty string - CSharp System

CSharp examples for System:Char

Description

Returns the last four characters of the string; if null, returns empty string

Demo Code

// Copyright (c) 2012 Computer Technology Solutions, Inc.  ALL RIGHTS RESERVED
using System.Text.RegularExpressions;
using System.Text;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;/*from   w ww . j a  va2s . c om*/

public class Main{
        /// <summary>
        /// Returns the last four characters of the string; if null, returns empty string
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string LastFour(this string value)
        {
            return String.IsNullOrEmpty(value) ? ""
                : value.Length <= 4 ? value
                : value.Substring(value.Length - 4, 4);
        }
}

Related Tutorials