Java String endsWith()

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.

Internal implementation
public boolean endsWith(String suffix) { return startsWith(suffix, value.length - suffix.value.length); }

The syntax or signature of endsWith() method is given below.

public boolean endsWith(String suffix)

suffix : Sequence of character


true or false

Java String endsWith() method example
public class Test1 { public static void main(String args[]) { String s1="java by cprogramcoding"; System.out.println(s1.endsWith("g")); System.out.println(s1.endsWith("coding")); } }

true true
Java String endsWith() Method Example
public class Test1 { public static void main(String[] args) { String str = "Welcome to"; System.out.println(str.endsWith("point")); if(str.endsWith(".com")) { System.out.println("String ends with .com"); }else System.out.println("It does not end with .com"); } }

false String ends with .com