C# - String.EndsWith() Method with Example

Learn, how to check whether string ends with given substring or not using String.EndsWith() in C#?
[Last updated : March 20, 2023]

String.EndsWith() Method

String.EndsWith() method checks whether string is ending with given substring or not?

Syntax

Bool String.EndsWith(String subStr);

Parameter(s)

  • subStr is the substring to be checked.
  • Bool is the Boolean value, it's a return type of this method, if string is ending with substring subStr method will return true and if it is not ending with substring subStr method will return false.

C# program to check whether string ends with given substring or not

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      String str1;
      bool flag = false;

      Console.Write("Enter String : ");
      str1 = Console.ReadLine();

      flag = str1.EndsWith("india");

      if (flag == true)
        Console.WriteLine("String ends with india");
      else
        Console.WriteLine("String does not end with india");

    }
  }

}

Output

Enter String : It is india
String ends with india

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.