How to get last four characters from a string in C#?

This post will provide a simple tip showing how you can get the last 4 characters from a string in C#.

How to get last four characters from a string in C#?

Assume that you have a string that contains the value “CodersEditor.com” and you wish to get .com from the string and display it.

Below is a sample code snippet demonstrating how you can do it.

public class Hello {
    public static void Main() {
        string website = "coderseditor.com";
        string result = website.Substring(website.Length - 4);;
        System.Console.WriteLine(result);
    }
}
%d