Open In App
Related Articles

String startswith() Method in Java with Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

In Java, startWith() method of the String class is present in java.lang package is used to check whether the string starts with a specific prefix. The package view is as follows:

 --> java.lang Package
    --> String Class
         --> startWith() Method   

Variants Of String startsWith() method

There are two variants of the startswith() method that are as follows:

  1. startsWith()
  2. startsWith(String prefix, int strt_pos)

1. String startsWith() method

This method tests if a string starts with the specified prefix beginning from the first index.

Syntax

public boolean startsWith(String prefix)

Parameters

The prefix to be matched.

Return Type

A boolean value that returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string else returns false.

Example:

Java




// Java Program to Illustrate startsWith() Method
// of String class
 
// Importing required classes
import java.util.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating custom string
        String str = new String(
            "GeeksforGeeks is Computer Science Portal");
 
        // Checking whether string starts with
        // corresponding input string passed
        // using startsWith() method and
        // later storing result in boolean value
        boolean ans1 = str.startsWith("Geeks");
        boolean ans2 = str.startsWith("Computer");
 
        // Printing the boolean value
        System.out.println(ans1);
        System.out.println(ans2);
    }
}


Output

true
false

2. String startsWith(String prefix, int strt_pos) method

This variant has two arguments and tests if a string starts with the specified prefix beginning a specified index. Here we are passing a starting position to be matched which allows us the flexibility  

Syntax

public boolean startsWith(String prefix, int strt_pos)

Parameters:

  • prefix: The prefix is to be matched.
  • strt_pos: Start position where to begin looking in the string.

Return Type: A boolean value that returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise.

Example:

Java




// Java Program to Demonstrate Working of startsWith()
// Method of String Class
 
// Importing required classes
import java.lang.String;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Declaring and initialising custom string
        String Str = new String("Welcome to GeeksforGeeks");
 
        // Display message
        System.out.print(
            "Check whether string starts with Welcome at pos 11 : ");
        // Testing the prefix using startsWith() method
        System.out.println(Str.startsWith("Welcome", 11));
 
        // Display message
        System.out.print(
            "Check whether string starts with geeks at pos 11 : ");
        // Testing the prefix using startsWith() method
        System.out.println(Str.startsWith("Geeks", 11));
    }
}


Output

Check whether string starts with Welcome at pos 11 : false
Check whether string starts with geeks at pos 11 : true

Real-Life Example

Now let us look up possible applications seen in daily life. This method can primarily be useful to filter prefixes. For example: filtering phone numbers starting from a number of names starting from a particular letter. The latter one is explained in this article. 

Example

Java




// Java Program to Illustrate startsWith() Method
// As Real-time Scenario
 
// Importing required classes
import java.util.*;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Declaring and initialising a string
        String Str = new String("Sandeep Jain");
 
        // Testing the prefix using startsWith() method
        System.out.print(
            "Check Whether It Starts With Letter 'S' : ");
 
        // Printing corresponding boolean value
        System.out.println(Str.startsWith("S"));
    }
}


Output

Check Whether It Starts With Letter 'S' : true

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above Add Comment Collapse.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 07 Jun, 2023
Like Article
Save Article
Similar Reads
Related Tutorials