C# Tutorial C# Advanced C# References

C# String - EndsWith() Method



The C# EndsWith() method is used to check whether the string ends with specified value or not. It returns true if the string ends with the specified value, else returns false.

Note: This method can be overloaded by passing different type of arguments to it.

Syntax

public bool EndsWith(char value);
public bool EndsWith(string value);
public bool EndsWith(string value, StringComparison comparisonType);
public bool EndsWith(string value, bool ignoreCase, 
                       System.Globalization.CultureInfo culture);

Parameters

value Specify string or char to compare.
comparisonType Specify the StringComparison enumeration value.
ignoreCase Specify true to ignore case during comparison, false otherwise
culture Specify culture information that determines how the string and value is compared. If it is null, current culture is used.

Return Value

Returns true if the string ends with the specified value, else returns false.

Exception

  • Throws ArgumentNullException, if the value is null.
  • Throws ArgumentException, if the comparisonType is not a StringComparison value.

Example:

In the example below, EndsWith() method is used to check whether the string ends with specified value or not.

using System;
using System.Globalization;

class MyProgram {
  static void Main(string[] args) {
    string MyStr = "HELLO World";

    Console.WriteLine(MyStr.EndsWith("RLD"));

    Console.WriteLine(MyStr.EndsWith("RLD", 
          StringComparison.CurrentCultureIgnoreCase)); 

    Console.WriteLine(MyStr.EndsWith("RLD", true, 
          new CultureInfo("en-US"))); 
  }
}

The output of the above code will be:

False
True
True

❮ C# String Methods