Java String startsWith() method with examples

This post will discuss the startsWith() method of the String class in detail. startsWith() helps us determine whether the string starts with the specified string. We have also kept some fun what-if scenarios at the end of the post.

In Java, we have two overloaded startsWith() methods –

Let’s look at both of them one by one.

public boolean startsWith(String prefix)

  • What does it do? This method will check whether the string begins with the specified string or not.
  • What does it return? It will return true if the string starts with the specified string. Otherwise, it will return false.
Code Example
public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru";

		String str2 = "code";
		String str3 = "hello";

		System.out.println("does str1 starts with str2? " + str1.startsWith(str2));
		System.out.println("does str1 starts with str3? " + str1.startsWith(str3));

	}
}

Output –

does str1 starts with str2? true
does str1 starts with str3? false

public boolean startsWith(String prefix, int toffset)

  • What does it do? It will accept two arguments – string and an offset. It will search for the string passed in the arguments starting from the specified offset.
  • What does it return? It will return true if the string is found starting from the specified offset. Otherwise, it will return false.
    • Remember, the offset should be passed based on 0-indexing. So, if you want to start from the 2nd element ( included ), you’ll have to pass 1 as an offset into the arguments.
public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru";

		String str2 = "code";

		System.out.println("does str1 starts with str2 starting from 3rd element? " + str1.startsWith(str2, 2));

	}
}

Output –

does str1 starts with str2 starting from 3rd element? false

We can see that the method returned false even though the string was present. This is because the startsWith() method searched the string from the 3rd position till the end. So, it searched “code” in the “dekru” string and thus returned false.

Do you know that startsWith(String prefix) internally uses the startsWith(String prefix, int toffset) method?

startsWith(String prefix) uses the startsWith(String prefix, int toffset) with toffset =0. Below is the internal implementation of the method.

    public boolean startsWith(String prefix) {
        return startsWith(prefix, 0);
    }
Time Complexity of startsWith() method

The average time complexity of the startsWith() method is O(n), where n is the length of the prefix.

The What If scenarios

What if we try to use the startsWith() method on a null String?

It will give us a NullPointerException, as shown in the below program.

public class Codekru {

	public static void main(String[] args) {

		String str1 = null;

		String str2 = "code";

		System.out.println("does str1 starts with str2? " + str1.startsWith(str2));

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.startsWith(String)" because "str1" is null
What if send a negative offset or offset > ( string’s length – prefix’s length) into the method arguments?

It will return false in both scenarios, as there is a check for these cases in the function’s definition.

        if (toffset < 0 || toffset > length() - prefix.length()) {
            return false;
        }

Let’s look at this with an example now.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru";

		String str2 = "code";

		System.out.println(str1.startsWith(str2, -1)); // sending negative offset
		System.out.println(str1.startsWith(str2, 20)); // sending positive offset

	}
}

Output –

false
false
What if we try to use the startsWith() method with an empty String?

It will return true in this scenario, as illustrated by the following example.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru";

		String str2 = "";

		System.out.println("Does str1 starts with str2? " + str1.startsWith(str2));

	}
}

Output –

Does str1 starts with str2? true
What if we try to pass the null String into the startsWith() method argument?

Yeah, you guessed it right. We will get the NullPointerException here.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru";

		String str2 = null;

		System.out.println("Does str1 starts with str2? " + str1.startsWith(str2));

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "prefix" is null
	at java.base/java.lang.String.startsWith(String.java:1453)
	at java.base/java.lang.String.startsWith(String.java:1496)

If you want to learn more about Strings and its methods, we recommend you read this article.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

References –

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *