java.lang.String startsWith()

Description :

This java tutorial shows how to use the startsWith() method of java.lang.String class. This method returns booelan data type. The startsWith() method of String class generally checks if the beginning of the String is equivalent to String prefix method argument.

Method Syntax :

public boolean startsWith(String prefix,int toffset)
public boolean startsWith(String prefix)

Parameter Input :

DataType Parameter Description
String prefix the string argument which we want to check at the beginning of our String
int toffset index of our String argument prefix where we want to start comparing with our String

Method Returns :

This method returns boolean. Method returns true if the character sequence specified on our parameter prefix matches as that our the starting characters of our String.

Java Code Example :

This example source code demonstrates the use of startsWith() method of String class. Basically this source code just checks if the String startswith the string declared on the parameter.


 * example java source code on the use of startsWith() method of String class
public class StringStartsWith {

    public static void main(String[] args) {
        // String object initialization
        String stringExample = "example startswith";

        // sample check using the startswith method
        if (stringExample.startsWith("exam")) {
            System.out.println("The string " + stringExample
                    + " starts with exam");
        } else {
            System.out.println("starts with argument return false");

        if (stringExample.startsWith("a", 10)) {
            System.out.print("Success check in using offset");
        } else {
            System.out.println("No match at all");


Sample Output :

Running the startsWith() method example source code of java.lang.String class will give you the following output

java string startswith method example

java string startswith method example

 Suggested Reading List :