Java String startsWith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, startsWith() returns true when a string begins with the given prefix, and false otherwise. The comparison is exact and case-sensitive.

The second overload checks the prefix at a chosen offset instead of index 0, which is handy for fixed-format text such as "ID:12345". It is the mirror image of endsWith().

startsWith() is an instance method of java.lang.String, available since JDK 1.0. Call it on a string, as in text.startsWith(...); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed.

Syntax

Pass the prefix, and optionally the index where it should appear.

java
boolean secure  = url.startsWith("https://");
boolean comment = line.startsWith("#");
boolean code    = record.startsWith("12", 3); // "12" at index 3?
CallWhat it does
"Java".startsWith("Ja")true.
"Java".startsWith("ja")false: case matters.
"Java".startsWith("va", 2)true: "va" starts at index 2.
"Java".startsWith("")true: every string starts with the empty string.

s.startsWith(p, k) gives the same answer as s.substring(k).startsWith(p) for a valid k, but creates no new string and returns false (instead of throwing) when k is negative or larger than the length.

Library declarations
java
public boolean startsWith(String prefix);
public boolean startsWith(String prefix, int toffset);

Parameters

ParameterDescription
prefixThe text the string should begin with. Must not be null.
toffsetOptional. The index where the prefix should start. Defaults to 0.

Return Value

Return ValueDescription
trueThe characters at the offset equal the prefix (always true for an empty prefix at a valid offset).
falseOtherwise, including when the offset is out of range.

Java Examples

Example 1 — Check Whether a URL Uses HTTPS

Test three addresses against the same prefix, including one written in capitals.

java
public class StartsWithUrl {
    public static void main(String[] args) {
        String[] urls = {"https://codetofun.com", "http://old.example", "HTTPS://LOUD.EXAMPLE"};

        for (String url : urls) {
            System.out.println(url + " secure? " + url.startsWith("https://"));
        }
    }
}

How It Works

  1. Exact prefix match. The first URL begins with the eight characters "https://", so the result is true.
  2. One letter short. "http://" has no s before the colon, so the prefix does not line up and the answer is false.
  3. Capitals fail. "HTTPS://" is a different sequence of chars; startsWith never ignores case.

Example 2 — Check a Prefix in the Middle with toffset

Read fixed-format records without cutting them up first.

java
public class StartsWithOffset {
    public static void main(String[] args) {
        String record = "ID:12345";
        String date = "2026-11-19";

        System.out.println(record.startsWith("ID:"));
        System.out.println(record.startsWith("12", 3));
        System.out.println(date.startsWith("11", 5));
        System.out.println(date.startsWith("11", 50));
    }
}

How It Works

  1. Offset 0 is the default. startsWith("ID:") checks the first three characters of the record.
  2. Jump to a field. With offset 3 the check begins right after "ID:", and offset 5 lands on the month "11" in the date.
  3. Out of range is just false. Offset 50 is past the end of the 10-character date, and the method returns false instead of throwing.

Example 3 — Skip Comment Lines in a Config

Ignore every line that starts with #, and see why an empty prefix always matches.

java
public class StartsWithFilter {
    public static void main(String[] args) {
        String[] lines = {"# settings", "port=8080", "#debug=true", "host=localhost"};

        for (String line : lines) {
            if (!line.startsWith("#")) {
                System.out.println("use: " + line);
            }
        }
        System.out.println("empty prefix: " + "port".startsWith(""));
    }
}

How It Works

  1. Negate the test. !line.startsWith("#") keeps only the lines whose first character is not a hash.
  2. Both comment styles drop out. "# settings" and "#debug=true" start with #, so only the port and host lines are printed.
  3. The empty prefix rule. Zero characters always match, so "port".startsWith("") is true; filter out empty prefixes yourself if that would be wrong.

Frequently Asked Questions

It returns true if the string begins with the given prefix and false otherwise. The comparison is case-sensitive.
Call startsWith(prefix, offset). It checks whether the prefix appears starting at that index, for example "ID:123".startsWith("12", 3) is true.
Yes. "Java".startsWith("ja") is false. Use regionMatches(true, 0, prefix, 0, prefix.length()) to ignore case.
It returns true for any string, because every string starts with the empty string.
Only NullPointerException, when the prefix or the string itself is null. A bad offset just returns false.

Next: strip()

Remove Unicode whitespace from both ends (Java 11+).

strip() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful