Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Java string endsWith() Method

Posted in Java Tutorial
Updated on Nov 20, 2024
By Mari Selvan
👁ī¸ 74 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
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:

Syntax
Copied
Copy To Clipboard
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.

EndsWithExample.java
Copied
Copy To Clipboard
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

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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
9 months ago

If you have any doubts regarding this article (Java string endsWith() Method), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy