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); }
Signature

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

public boolean endsWith(String suffix)
Parameter

suffix : Sequence of character

Returns

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")); } }


Output:
true true
Java String endsWith() Method Example
public class Test1 { public static void main(String[] args) { String str = "Welcome to cprogramcoding.com"; 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"); } }


Output:
false String ends with .com



Instagram