Library contents

Java String.startsWith

Check the beginning of a reference before routing it to the billing queue.

A simple example

String.startsWith

  1. Start with "abc" and "ab"
  2. Do this Compare the beginning
  3. Result Yes
The first two characters match the requested prefix.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println("abc".startsWith("ab"));
    }
}
Output
true

Syntax

Java syntax
label.startsWith("INV-")
JDK 21 method signatures (2)
public boolean startsWith(String prefix, int toffset)
public boolean startsWith(String prefix)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Recognize an invoice prefix

A finance inbox recognizes invoices by a specific prefix on their reference codes. Inspect the beginning of the code before placing an incoming record in the invoice queue.

From the programString label = "INV-482"
Resultyes
Main.java

class Main {
    public static void main(String[] args) throws Exception {
        String label = "INV-482";
        System.out.println((label.startsWith("INV-")) ? "yes" : "no");
    }
}
Output
yes

The first four characters are INV-, so this order enters the invoice queue.

Advertisements

Route a support ticket

An intake queue uses a recognizable prefix or status to route incoming work. Check the required position and comparison rules so a matching fragment in an unrelated field cannot send a request to the wrong desk.

From the programString ticket = "HR-125"
Resulttrue
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String ticket = "HR-125";
        System.out.println(ticket.startsWith("HR-"));
    }
}
Output
true

The prefix identifies the human-resources queue.

Check the campus field after a separator

A shipping or campus label contains several fields with fixed boundaries. Match only the selected field and allow for short input that might end before all expected characters are present.

From the programString card = "ID:LAB-21"
Resulttrue
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String card = "ID:LAB-21";
        System.out.println(card.startsWith("LAB-", 3));
    }
}
Output
true

Offset three skips ID: and checks the next characters.

What to watch for

Prefix matching here is case-sensitive. Keep the full identifier when handing it to the next step.

Documentation for String.startsWith