BlogsDope image BlogsDope

Java String endsWith() Method

March 18, 2021 JAVA FUNCTION STRING 1158

The Java endsWith() method is a case-sensitive method used to check whether a string is ending with a user-specified string or not. 

Java endsWith() Example

  • Input: "How are you".endsWith("you")

       Output: true     //"you" is the last word of the input string.

  • Input: "How are you".endsWith("are")

       Output: false   //"are" is not the last word of the input string.

Java endsWith() Syntax

"String".endsWith(suffix);

Java endsWith() Parameters

suffix-string that you want to search in the sentence.

Java endsWith() Return

This method returns a boolean value-

Returns true if the string ends with the given suffix

Returns false if the string doesn't ends with the given suffix.

Java endsWith() Program

package codesdope;
import java.util.*;
public class codesdope 
{
	public static void main(String[] args) 
	{
		String s1="Welcome to Codesdope";
		System.out.println(s1.endsWith("ope"));
		System.out.println(s1.endsWith("Codesdope"));
		System.out.println(s1.endsWith("to"));
	}
}

​true 

true 

false

  • In the first case, we are finding whether our string s1 i.e "Welcome to Codesdope" is ending with ope or not using endsWith() method and it returned true because "ope" are the last three characters of the string s1.
  • In the second case, we are finding whether our string s1 i.e "Welcome to Codesdope" is ending with "Codesdope" or not using endsWith() method and it returned true because "codesdope" is the last word of the string.
  • In the last case, we are finding whether our string s1 i.e. "Welcome to Codesdope" is ending with "to" or not using endsWith() ​method and it returned false because "to" comes in between the sentence not at the end.

Liked the post?
Editor's Picks
0 COMMENT

Please login to view or add comment(s).