Site icon TestingDocs.com

Java endsWith() method

Java endsWith() method

Java endsWith() method tests if the string ends with the specified suffix. It returns true if the string ends with given suffix else returns false.

endsWith method signature

public boolean endsWith(String suffix)

suffix: The sequence of characters to check at the end of the string.

Example

Let’s understand the method usage with the help of an example:

package com.testingdocs.stringmethods;

public class ExampleProgram {

	public static void main(String[] args) {
		String str= "www.TestingDocs.com";
		
		// endsWith() method demo
		System.out.println("str ends with .com ? = "+ str.endsWith(".com"));
		System.out.println("str ends with docs ? = "+ str.endsWith("docs"));
	}
}

Program Output

The sample Java program produces the following output:

str ends with .com ? = true

str ends with docs ? = false

 

Java Tutorials

Java Tutorial on this website:

For more information on Java, visit the official website :

Exit mobile version