In C#, Substring method is used to retrieve (as the name suggests) substring from a string. The same we can use to get the last ‘N’ characters of a string.
String.Substring Method
As described in String.Substring Method (System) | Microsoft Docs, Substring method has two overloads,
Overload | Description |
Substring(Int32) | Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string. |
Substring(Int32, Int32) | Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length. |
With the starting position only overload, we can do.
mystring.Substring(mystring.Length - N);
But the above code will fail in case mystring
length is lower than the ‘N’. So considering that case, lets have an extension method,
public static string GetLast(this string source, int numberOfChars)
{
if(numberOfChars >= source.Length)
return source;
return source.Substring(source.Length - numberOfChars);
}
And you can use the above like,
mystring.GetLast(5)
This extension method is now part of the Code.Library Nuget package.