This post will discuss how to determine whether a string ends with another string in C#.

1. Using String.EndsWith() method

The standard method to determine whether a string ends with another string is to use the String.EndsWith() method. It returns true if the end of the string matches with the specified string. This is demonstrated below:

Download  Run Code

2. Using Enumerable.Any() method

If you need to match the end of a string with the list of given strings, consider using LINQ’s Enumerable.Any() method. It returns true when any element of a sequence satisfy the specified condition. The following code example shows how to test a suffix in a list of strings using the Any() and EndsWith() method.

Download  Run Code

3. Using Regex.IsMatch() method

Alternatively, you can use the regular expression to match the end of a string with the list of strings. This can be done using the Regex.IsMatch() method, which tells whether the specified string matches the provided regex. This is demonstrated below. Note that $ matches with the end of the string.

Download  Run Code

That’s all about determining whether a string ends with another string in C#.