C# Tutorial C# Advanced C# Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C# - String EndsWith() Method



The C# string 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

5