The endsWith() method in Java is used to check whether a string ends with a specific suffix. It returns true if the string matches the given ending and false otherwise. This method is useful for validating file extensions, URLs, and other string-based checks.
In this blog, we will learn how we can use the endsWith() method to compare strings in Java:
Table of Contents:
What is endsWith() Method in Java?
The endsWith() method in Java is a built-in method of the String class that checks whether a string ends with a specified suffix. It returns true if the string ends with the given substring; otherwise, it returns false. This method is case-sensitive.
Syntax of endsWith() in Java
boolean result = string.endsWith(suffix);
Parameters of endsWith() in Java
Here are the parameters of the endsWith() in Java:
- string: The string to check.
- suffix: The substring that you want to check at the end of the string.
- result: Boolean value (true if the condition is met, otherwise false)
Return Value of endsWith() in Java
It returns a boolean value(either true or false).
- true: If the given string ends with the specified suffix.
- false: If the given string does not end with the specified suffix.
Exceptions of endsWith() in Java
The endsWith() method in Java belongs to the String class and has a simple behavior when handling exceptions. It can throw:
- NullPointerException: This exception occurs if the argument passed to endsWith() is null.
- incompatible types compilation error: if you pass a non-string argument to the endsWith() method.
Examples of endsWith() Method in Java
Here are the examples of endswith() to compare strings in Java:
Example 1: Checking for Case Sensitivity in Java
endsWith() is case-sensitive, meaning “Hello”.endsWith(“lo”) returns true, but “Hello”.endsWith(“LO”) returns false.
Example:
Output:
Explanation: This program checks if a string or filename ends with a specific word or extension using endsWith(). It is case-sensitive, so “Intellipaat” and “intellipaat” are treated differently.
Example 2: Checking with a Different Suffix
We can check different suffixes and see if they match in Java or not.
Example:
Output:
Explanation: In the above code, we have checked for a particular suffix in the string.
- str.endsWith(“Java”) returns true as it ends with the Java suffix.
- str.endsWith(“C++”) returns false as it does not end with a C++ suffix.
Example 3: Using endsWith() with an Empty String
An empty string (” “) is considered a suffix of any string, so it will return true always.
Example:
Output:
Explanation: Any string always ends with “” (empty string). So the output is true in the above case.
Example 4: Using endsWith() for File Extensions
We can also check if a filename ends with a specific extension or not in Java:
Example:
Output:
Explanation: We can also check if a particular file is of a specific extension or not, in the above program, we have checked if filename1 and filename2 are pdf files.
- fileName1.endsWith(“.pdf”): It returns true as filename1 is a pdf file
- fileName2.endsWith(“.pdf”): it returns false as filename2 is not a pdf file.
Example 5: Check String Ends with a Space
A space at the end of a string affects endsWith().
Example:
Output:
Explanation: In the above Program, we have two strings str1 and str2, one is having the trailing space, and the other one is not.
- “Hello “.endsWith(” “): It returns true (as it contains a trailing space).
- “Hello”.endsWith(” “): It returns false (as it does not contain a trailing space).
Example 6: Validating a Popular Domain
We can check if a URL ends with a specific domain (.com, .org).
Example:
Output:
Explanation: In the above program, we have checked for a particular domain in the string:
- “www.example.com”.endsWith(“.com”): It returns true as the URL contains .com.
- “www.example.com”.endsWith(“.org”): It returns false as the URL does not contain .org.
Conclusion
So far in this blog, we have learned how we can use the endsWith() method to check if a string ends with a specific suffix. This method is case-sensitive and is often used for verifying file extensions, URLs, and other string validations. It returns true if the string ends with the given suffix, otherwise, it returns false.
If you want to be an expert in Java Programming language, you may refer to our Java Course.
Some Other Methods to Compare Strings in Java