Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 endsWith() method and the startsWith() method of the String class in java.

Let us discuss both the methods individually:

  1. endsWith() Method in java
  2. startsWith() 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 endsWith() 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";
 
        // 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 startsWith() 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

 



Last Updated : 27 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads