Get last 3 characters of string

How can I get only the last 3 character out from a given string?

Example input: AM0122200204

Expected result: 204


Many ways this can be achieved.

Simple approach should be taking Substring of an input string.

var result = input.Substring(input.Length - 3);

Another approach using Regular Expression to extract last 3 characters.

var result = Regex.Match(input,@"(.{3})\s*$");

Working Demo


The easiest way would be using Substring

string str = "AM0122200204";
string substr = str.Substring(str.Length - 3);

Using the overload with one int as I put would get the substring of a string, starting from the index int. In your case being str.Length - 3, since you want to get the last three chars.


From C# 8 Indices and ranges

Last 3 digits of "AM0122200204" string:

"AM0122200204"[^3..]