String - RightSafe

A string extension method that return the right part of the string safely, if length passed as parameter is greater than the length of the string object, then orginal string is returned.

Try it

public static void Main()
{
	string input = "OneTwoThree";

	// C# Extension Method: String - RightSafe
    string newStr = input.RightSafe(5);
	Console.WriteLine(newStr);
	
	// C# Extension Method: String - RightSafe
    newStr = input.RightSafe(15);
	Console.WriteLine(newStr);
}

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     A string extension method that right safe.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="length">The length.</param>
    /// <returns>A string.</returns>
    public static string RightSafe(this string @this, int length)
    {
        return @this.Substring(Math.Max(0, @this.Length - length));
    }
}