How to Check If a String Ends with Another String in Java


Here this example will use the endsWith() method in the String object to determine whether it ends with a String input value.

Source Code

package com.beginner.examples;

public class EndsWithString {

	public static void main(String[] args) {
		String str = "I love java";

		// Retrieves whether the string ends with "java"
		boolean b = str.endsWith("java");

		boolean b2 = str.endsWith("love");

		System.out.println("Is this string end with an "java": " + b);

		System.out.println("Is this string end with an "love": " + b2);

	}

}

Output:

Is this string end with an "java": true
Is this string end with an "love": false
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments