
Right is a custom extension method. It obtains the rightmost several characters of an input string and returns them as a new string. The .NET Framework does not provide a Right method on the string type. We add one with extension method syntax.
This C# article implements the Right extension method. Right returns the rightmost part of a string.

First, this program implements the string type Right method using the extension method syntax in newer versions of the C# language. The Right method is called on a string instance and it internally returns a substring of the right-most several characters. The call Right(3) will then return the final three characters in the string. Like other C# strings, the null character is not returned.
Program that implements Right [C#]
using System;
static class Extensions
{
/// <summary>
/// Get substring of specified number of characters on the right.
/// </summary>
public static string Right(this string value, int length)
{
return value.Substring(value.Length - length);
}
}
class Program
{
static void Main()
{
const string value1 = "dot net perls";
const string value2 = "SAM ALLEN";
string result1 = value1.Right(5);
string result2 = value2.Right(1);
Console.WriteLine(result1);
Console.WriteLine(result2);
}
}
Output
perls
NExtension method syntax. The program introduces the Extensions static class and the Program class. The Extensions class is where the Right extension method is declared. The Right extension method is exactly like a public static method, but the first parameter must use the "this" modifier before the type declaration. The Main entry point can then call the Right method we defined on any string type.
Extension Method
Exceptions thrown. The exceptions thrown by the Right extension method are similar to those thrown by the Substring method, and Substring will be the source of the exceptions. If you provide a number that is too large or the input string is null, you will receive an exception. This preserves the behavior of other string methods. You will need to check the string length before using Right if you may have undersized strings.

We implemented the string type Right method in the C# language, which returns a new string instance containing the specified number of characters starting from the final character index. The Right method is available in many languages, but is not built into the .NET string type, and this simple wrapper can provide a way to translate logic more effectively. It is similar to a Truncate method.
Truncate String String Type