Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
StringBuilder delete() in Java with Examples
Next article icon

Java String endsWith() with Examples

Last Updated : 20 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the endsWith() method of the String class is used to check if a string ends with a specific suffix. The endsWith() method is present in the java.lang package. In this article, we will learn how to use the endsWith() method in Java and explore its practical examples.

Example:

In this example, we will use the endsWith() method to check whether the string ends with a specific suffix.

Java
// Java program to demonstrate endsWith() method  public class StringEndsWith {      public static void main(String[] args) {          // Declare and initialize a String         String s = "Java Programming";          // Check if the string ends with "Programming"         boolean r = s.endsWith("Programming");          System.out.println("" + r);     } } 

Output
true 

Table of Content

  • Syntax of endsWith() method
  • Other Examples of String endsWith() Method
    • Case Sensitivity Example
    • Checking with a Different Suffix
    • Using endsWith() with an Empty String
    • Using endsWith() for File Extensions
    • Check String Ends with a Space

Syntax of endsWith() method

boolean endsWith(String suffix)

Parameter:

  • suffix: The suffix to be checked.

Return Type:

  • If the string ends with the given suff, it returns true.
  • If the string does not end with the stuff, it returns false.

Other Examples of String endsWith() Method

1. Case Sensitivity Example

The endsWith() method is case-sensitive. This means that it will return false if the case of the characters does not match the specified suffix.

Example:

Java
// Java program to demonstrate case sensitivity of endsWith()  public class StringEndsWith {      public static void main(String[] args) {          // Declare and initialize a String         String s = "Java Programming";          // Check if the string ends with "programming" (lowercase)         boolean r = s.endsWith("programming");          System.out.println("" + r);     } } 

Output
false 

2. Checking with a Different Suffix

We can also check whether the string ends with any suffix.

Example:

Java
// Java program to demonstrate endsWith() with a different suffix  public class StringEndsWith {      public static void main(String[] args) {          // Declare and initialize a String         String s = "Java Programming";          // Check if the string ends with "Programming"         boolean r = s.endsWith("Programming");          System.out.println("" + r);     } } 

Output
true 

3. Using endsWith() with an Empty String

We can check if a string ends with an empty string or not. It will always return true, because every string ends with an empty string.

Example:

Java
// Java program to demonstrate endsWith() with an empty string  import java.util.*;  public class StringEndsWith {      public static void main(String[] args) {          // Declare and initialize a String         String s = "Java Programming";          // Check if the string ends with an empty string         boolean r = s.endsWith("");          System.out.println("" + r);     } } 

Output
true 


4. Using endsWith() for File Extensions

The endsWith() method used for checking file extensions. We can check if a file name ends with .txt or anything else.

Example:

Java
// Java program to demonstrate endsWith() with file extension import java.util.*;  public class StringEndsWith {      public static void main(String[] args) {          // Declare and initialize a String representing file name         String fileName = "document.txt";          // Check if the file name ends with ".txt"         boolean r = fileName.endsWith(".txt");          System.out.println("" + r);     } } 

Output
true 


5. Check String Ends with a Space

We can also check if a string ends with a space using the endsWith() method.

Example:

Java
// Java program to demonstrate the working of endsWith() method  class StringEndsWith {      public static void main(String args[]) {                String s = "Java Programming";          // Output will be false if the argument         // is the empty space         boolean b = s.endsWith(" ");         System.out.println(b);     } } 

Output
false 



Next Article
StringBuilder delete() in Java with Examples

N

Niraj_Pandey
Improve
Article Tags :
  • Java
  • Java-Functions
  • Java-Strings
Practice Tags :
  • Java
  • Java-Strings

Similar Reads

  • Java String concat() Method with Examples
    The string concat() method concatenates (appends) a string to the end of another string. It returns the combined string. It is used for string concatenation in Java. It returns NullPointerException if any one of the strings is Null. In this article, we will learn how to concatenate two strings in Ja
    4 min read
  • Path endsWith() method in Java with Examples
    endswith() method of java.nio.file.Path used to check if this path object ends with the given path or string which we passed as parameter. There are two types of endsWith() methods. endsWith(String other) method of java.nio.file.Path used to check if this path ends with a Path, constructed by conver
    3 min read
  • StringBuilder delete() in Java with Examples
    The delete(int start, int end) method of StringBuilder class removes the characters starting from index start to index end-1 from String contained by StringBuilder. This method takes two indexes as a parameter first start represents index of the first character and endIndex represents index after th
    3 min read
  • Java String contains() Method with Example
    The String.contains() method is used to search for a sequence of characters within a string. In this article, we will learn how to effectively use the string contains functionality in Java. Example: In this example, we check if a specific substring is present in the given string. [GFGTABS] Java // J
    3 min read
  • Stream anyMatch() in Java with examples
    Stream anyMatch(Predicate predicate) returns whether any elements of this stream match the provided predicate. It may not evaluate the predicate on all elements if not necessary for determining the result. This is a short-circuiting terminal operation. A terminal operation is short-circuiting if, wh
    2 min read
  • StringBuilder charAt() in Java with Examples
    The charAt(int index) method of StringBuilder Class is used to return the character at the specified index of String contained by StringBuilder Object. The index value should lie between 0 and length()-1. Syntax: public char charAt(int index) Parameters: This method accepts one int type parameter in
    3 min read
  • StringBuilder deleteCharAt() in Java with Examples
    The deleteCharAt(int index) method of StringBuilder class remove the character at the given index from String contained by StringBuilder. This method takes index as a parameter which represents the index of char we want to remove and returns the remaining String as StringBuilder Object. This StringB
    3 min read
  • CompoundName endsWith() method in Java with Examples
    The endsWith() method of a javax.naming.CompoundName class is used to check whether compound name which is passed as a parameter is a suffix of this compound name or not. A compound name 'X' is a suffix of this compound name if this compound name object ends with 'X'. If X is null or not a compound
    2 min read
  • CompositeName endsWith() method in Java with Examples
    The endsWith() method of a javax.naming.CompositeName class is used to check whether composite name which is passed as a parameter is a suffix of this composite name or not. A composite name 'X' is a suffix of this composite name if this composite name object ends with 'X'. If X is null or not a com
    2 min read
  • Matcher end() method in Java with Examples
    The end() method of Matcher Class is used to get the offset after the last character matched of the match result already done. Syntax: public int end() Parameters: This method do not takes any parameter. Return Value: This method returns the offset after the last character matched Exception: This me
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences