System.String.EndsWith Method

Determines whether the end of this string instance matches the specified string.

Syntax

public bool EndsWith (string value)

Parameters

value
The string to compare to the substring at the end of this instance.

Returns

true if value matches the end of this instance; otherwise, false.

Exceptions

TypeReason
ArgumentNullException value is a null reference.

Remarks

This method compares value to the substring at the end of this instance that is the same length as value, and returns an indication whether they are equal. To be equal, value must be a reference to this same instance or match the end of this instance.

This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture.

Example

The following example demonstrates determining whether the current instance ends with a specified string.

C# Example

using System;
public class StringEndsWithExample {
 public static void Main() {
 string str = "One string to compare";
 Console.WriteLine( "The given string is '{0}'", str );
 Console.Write( "The given string ends with 'compare'? " );
 Console.WriteLine( str.EndsWith( "compare" ) );
 Console.Write( "The given string ends with 'Compare'? " );
 Console.WriteLine( str.EndsWith( "Compare" ) );
 }
}
   

The output is

The given string is 'One string to compare'
The given string ends with 'compare'? True
The given string ends with 'Compare'? False

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0