Java String EndsWith Example

The Java String.endsWith() method will test whether or not the current string ends with the specified suffix. The suffix is in the form of another string. The method will return true if the string suffix is found at the end of the current string. If suffix not found, it will return false.

Class Name Method Parameters Return Type Exceptions
String endsWith() String boolean none


Java String EndsWith Source Code

The three samples below of endsWith() method use a variation to test the suffix string “Com”. By adding a leading or trailing space to the suffix, it will change the result.

package com.jcd.java.tutorials;

public class JavaStringEndsWith 
{

	public static void main(String[] args) 
	{


		String jcdString1 = "Java Code Depot Com";
		boolean jcdBoolean1 = jcdString1.endsWith("Com");
		System.out.println(jcdString1+" ends with 'Com':"+jcdBoolean1);
		
		String jcdString2 = "Java Code Depot Com";
		boolean jcdBoolean2 = jcdString2.endsWith("Com ");
		System.out.println(jcdString2+" ends with 'Com ':"+jcdBoolean2);
		
		String jcdString3 = "Java Code Depot Com";
		boolean jcdBoolean3 = jcdString3.endsWith(" Com");
		System.out.println(jcdString3+" ends with ' Com':"+jcdBoolean3);
		
	}

}


Java String EndsWith Output

Java Code Depot Com ends with 'Com':true
Java Code Depot Com ends with 'Com ':false
Java Code Depot Com ends with ' Com':true


Add a Comment

Your email address will not be published. Required fields are marked *