Skip to content
Related Articles

Related Articles

Improve Article

Java String startsWith() and endsWith() Methods With Examples

  • Last Updated : 07 Aug, 2021

The String class in Java is an immutable and non-primitive data type that is used to store the characters of the sequence. The string is non-primitive datatype because, during the initialization of the string variable, it refers to an object that contains methods that can perform several different kinds of operations, but according to the definition of the primitive datatype, they are not considered as the object and store the data in the stack memory. In this article, we will discuss the endWith() method and the startsWith() method of the String class in java.

Let us discuss both the methods individually:

  1. enWith() Method in java
  2. startWith() Method

Method 1: endsWith() method 

This method of the String class checks whether the given string ends with the specified string suffix or not.

Syntax:



endsWith(String suffix)     

Parameter: This method takes one parameter that is of string type..

Return type: This method returns a boolean value true or false. the i.e string ends with a specified suffix or not.

Example:

Java




// Java Program to illustrate endWith() Method
 
// Importing required classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main drier method
    public static void main(String[] args)
    {
        // Given String
        String first = "Geeks for Geeks";
 
        // Suffix to be matched
        String suffix = "kse";
 
        // Given String does not end with
        // the above suffix hence return false
        System.out.println(first.endsWith(suffix));
 
        // Changing the suffix say
        // it be customly 's'
        suffix = "s";
 
        // Given String ends with the given suffix  hence
        // returns true
        System.out.println(first.endsWith(suffix));
    }
}
Output
false
true

Method 2: startsWith() method

This method of the String class checks whether the given string starts with the specified string prefix or not.

Syntax: 

startsWith(String prefix)     

Parameter: This method takes one parameter that is of string type..

Return type: This method returns a boolean value true or false. the i.e string ends with a specified prefix or not.

Examples: 

Java




// Java Program to illustrate startWith() Method
 
// Importing required classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Given String
        String first = "Geeks for Geeks";
 
        // Prefix to be matched
        String prefix = "se";
 
        // Given String does not start with the above prefix
        // hence return false
        System.out.println(first.startsWith(prefix));
 
        // Changing the prefix
        prefix = "Gee";
 
        // Given String starts with the given prefix hence
        // returns true
        System.out.println(first.startsWith(prefix));
    }
}
Output
false
true

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up