Java String Methods
Java string endsWith() Method
Photo Credit to CodeToFun
đ Introduction
In Java programming, working with strings is a common and fundamental task.
The endsWith()
method is a built-in string method that allows you to check whether a string ends with a specified suffix.
In this tutorial, we'll explore the usage and functionality of the endsWith()
method in Java.
đĄ Syntax
The syntax for the endsWith()
method is as follows:
public boolean endsWith(String suffix);
- suffix: The substring that you want to check if the string ends with.
đ Example
Let's dive into some examples to illustrate how the endsWith()
method works.
public class EndsWithExample {
public static void main(String[] args) {
String str = "Hello, World!";
// Check if the string ends with "World!"
boolean result1 = str.endsWith("World!");
System.out.println("Example 1: " + result1); // Output: true
// Check if the string ends with "Java"
boolean result2 = str.endsWith("Java");
System.out.println("Example 2: " + result2); // Output: false
}
}
đģ Output
Example 1: true Example 2: false
đ§ How the Program Works
In Example 1, the endsWith()
method checks if the string "Hello, World!" ends with the specified suffix "World!" and prints the result, which is true.
In Example 2, the method checks if the string ends with "Java" and prints the result, which is false.
âŠī¸ Return Value
The endsWith()
method returns a boolean value. It is true if the string ends with the specified suffix; otherwise, it is false.
đ Common Use Cases
The endsWith()
method is useful when you need to perform conditional operations based on the ending of a string. It's commonly used to check file extensions, validate user input, or filter strings based on specific criteria.
đ Notes
- The comparison is case-sensitive. If you need a case-insensitive check, consider converting both the string and the suffix to lowercase (or uppercase) using the toLowerCase() or toUpperCase() methods before calling
endsWith()
. - The
endsWith()
method is particularly handy when working with file paths to ensure that a file has a specific extension.
đĸ Optimization
The endsWith()
method is already optimized for efficiency. However, if you are dealing with extremely long strings or need to perform the check in a performance-critical section of your code, consider other optimization strategies or algorithms depending on your specific use case.
đ Conclusion
The endsWith()
method in Java provides a straightforward way to check whether a string ends with a specified suffix. It enhances the readability and conciseness of your code when performing such checks.
Feel free to experiment with different strings and suffixes to deepen your understanding of the endsWith()
method. Happy coding!
đ¨âđģ Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Java string endsWith() Method), please comment here. I will help you immediately.