EndsWith() Method in C#


The EndsWith() method in C# is used to check whether the ending of the current string instance matches with a specified string or not.

Syntax

Following is the syntax −

public bool EndsWith(string str)

Above, the str parameter is the string to be compared.

Example

Let us now see an example to implement the EndsWith() method −

using System;
public class Demo{
   public static void Main(){
      bool val;
      string str = "demo$";
      val = str.EndsWith("$");
      Console.WriteLine("Return Value = "+val.ToString());
   }
}

Output

This will produce the following output −

Return Value = True

Example

Let us now see another example to implement the EndsWith() method −

using System;
public class Demo{
   public static void Main(){
      bool val;
      string str = "mytext@";
      val = str.EndsWith("#");
      Console.WriteLine("Return Value = "+val.ToString());
   }
}

Output

This will produce the following output −

Return Value = False

Updated on: 08-Nov-2019

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements