Checking if a string ends with a number in C# is a very common operation. In this article, we are going to show various ways in which we can do this. 

To download the source code for this article, you can visit our GitHub repository.

Let’s start.

Check if a String Ends With a Number Using IsDigit

Perhaps the most straightforward way of checking if a string ends with a number is by using the char.IsDigit method. We can use this method by passing in the last character of our string. This method is also pretty fast:

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!
string inputString = "Hello1";
bool result = char.IsDigit(inputString.Last());

We can also modify this example to make it a bit faster:

bool result = char.IsDigit(inputString[^1]);

Check Using Character Comparison

A similar, yet slightly more complex method, is to again get the last character of the string and do a comparison with the 0 and 9 chars. Note that we use the digits as chars here, and not as integers, hence the apostrophes:

string inputString = "Hello1"; 
var lastChar = inputString.Last(); 

bool result = lastChar >= '0' && lastChar <= '9';

This is indeed slightly more complex, due to the comparison of characters, and also more prone to errors compared to the previous one-liner. However, it does allow one to finetune it further, e.g. by checking whether the last digit is equal to only 1, 2, or 3, but not the other digits.

Check Using Substring

The third way to check the same thing is by using the string.Substring method to get the last character of a string. The Substring method returns another string, to which we apply the IEnumerable.All method to check whether all elements (chars, in this case) satisfy a certain condition (char.IsDigit here):

string inputString = "Hello1"; 
bool result = inputString.Substring(inputString.Length - 1).All(char.IsDigit); 

Even though this is slightly slower compared to the first solution, it is more flexible in that it allows us to easily get the last n characters, simply by changing the 1 into n. This way we can do the same check for the last n characters. In case we take multiple characters, we might even change the All method into Any. This allows us to check whether there is at least one digit character in the last n characters. 

Using Regex to Check if a String Ends With a Number

Another way of doing this check is by using a regular expression:

string inputString = "Hello1"; 
var regex = new Regex(@"\d+$");
 
bool result = regex.Match(inputString).Success;

What we do is quite straightforward though: We create a new Regex checking whether the last character is a digit: $ matches the end of a string, while  \d matches a digit. Regular expressions are extremely powerful and not only allow us to check whether a string ends with a digit, but we can easily modify it to extract that digit too. Take a look at our article about regular expressions in C# if you need to learn more or refresh your memory about it.

This method is also extremely fast, similar to doing the same using the last character and applying the IsDigit method on it.

Conclusion

In this article, we explored different methods of checking whether a string ends with a number in C#. We saw that even though all of them produce the same result, different methods might be preferred if we want to do slightly different operations. 

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!