Java endsWith Method

The Java endsWith method is one of the Java String Methods, which is to check whether the string is ending with user-specified substring or not. Based on the result, endsWith method will return Boolean True or False.

In this article, we will show how to write string endsWith in Java Programming language with example. The syntax of the string.endsWith in Java Programming language is as shown below.

Java endsWith method syntax

The following Java endsWith method will accept the substring as the argument and check whether the string is ending with this substring or not

public boolean endsWIth(String suffix); // It will return boolean True or False 

//In order to use in program
String_Object.endsWIth(String suffix)

NOTE: If we specify the Suffix as an empty string, String.endsWith function returns Boolean True as output.

Java endsWith Example

In this Java program, We are going to use the Java string endsWith method to check whether the string is ending with user-specified substring or not.

// Java String.endsWith example
package StringFunctions;

public class EndsWithMethod {
	public static void main(String[] args) {
		String str = "Tutorials On Java Programming";
		String str1 = "We are abc working at abc company";
		
		boolean a = str.endsWith("ing");
		boolean b = str.endsWith("Programming");
		boolean c = str1.endsWith("company");
		boolean d = str1.endsWith("abc");
		
		System.out.println("Does the String str ends with ing? = " + a);
		System.out.println("Does the String str ends with Programming? = " + b);
		System.out.println("Does the String str1 ends with company? = " + c);
		System.out.println("Does the String str1 ends with abc? = " + d);
	}
}
Java endsWith Method 1

It will call the public boolean endsWith (String suffix) method to check whether the string str is ending with ing or not. If it is TRUE, it will return TRUE, otherwise False.

boolean a = str.endsWith("ing");

The following Java endsWith statement will check whether the string str1 is ending with the company or not. If it is TRUE, it will return TRUE, otherwise False.

boolean c = str1.endsWith("company");

Let us try the wrong value. The following statement will call the public boolean endsWith (String suffix) method to check whether the string str1 is ending with abc or not. We all know that it is False.

boolean d = str1.endsWith("abc");

The following System.out.println statements will print the output.

System.out.println("Does the String str ends with ing? = " + a);
System.out.println("Does the String str ends with Programming? = " + b);
System.out.println("Does the String str1 ends with company? = " + c);
System.out.println("Does the String str1 ends with abc? = " + d);

Java endsWith Example

In this Java program, We are going to ask the user to enter any word. Based on the user entered string value, the endswith program will display the message.

package StringFunctions;

import java.util.Scanner;

public class EndsWithMethod2 {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.println("Please Enter any word: ");
		String str = sc.nextLine();
		
		if (str.endsWith("java")) {
			System.out.println("Hey!! Welcome to Java Tutorials");
		}
		else {
			System.out.println("Goodbye to Java Programming");
		}
	}
}
Please Enter any word: 
hello java
Hey!! Welcome to Java Tutorials

Let us enter a different word

Please Enter any word: 
hi friends
Goodbye to Java Programming

Within the endsWith String Method example, the following statement will ask the user to enter any word. Then we are going to assign the user entered value to string variable str.

System.out.println("Please Enter any word: ");
String str = sc.nextLine();

Next, we used the Java If Else Statement to check where the user entered string is ending with “java” or not.

  • If the condition is True, the Java System.out.println(“Hey!! Welcome to Java Tutorials”); statement will be printed.
  • Else System.out.println(“Goodbye to Java Programming”); statement will be printed.