The Java endsWith method is one of the Java String Method which is used to check whether the string is ending with user specified substring or not, and based on the result it will return Boolean True or False.
In this article we will show you, How to write string endsWith in Java Programming language with example. Before we get into the example, let us see the basic 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 you specify the Suffix as an empty string, Java String.endsWith function will return Boolean True as output.
Java endsWith Example
The Java string endsWith method is used to check whether the string is ending with user specified substring or not. In this Java program, We are going to find the same.
JAVA CODE
// 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); } }
OUTPUT
ANALYSIS
First we declared two String variables Str, Str1 and assigned corresponding values using following statement
String str = "Tutorials On Java Programming"; String str1 = "We are abc working at abc company";
Following statement 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");
Following Java endsWith statement will check whether the string str1 is ending with company or not. If it is TRUE, it will return TRUE otherwise False
boolean c = str1.endsWith("company");
Let us try the wrong value. 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");
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 and based the user entered string value we will display the message.
JAVA CODE
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"); } } }
OUTPUT
Let us enter different word
ANALYSIS
Within the Java endsWith example, Following statement will ask the user to enter any word, and 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 System.out.println(“Hey!! Welcome to Java Tutorials”); statement will be printed
- Else System.out.println(“Goodbye to Java Programming”); statement will be printed
Thank You for Visiting Our Blog