Java String.startsWith
Check the beginning of a reference before routing it to the billing queue.
A simple example
String.startsWith
- Start with "abc" and "ab"
- Do this Compare the beginning
- Result Yes
class Main {
public static void main(String[] args) throws Exception {
System.out.println("abc".startsWith("ab"));
}
}
true
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.javaRecognize 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.
class Main {
public static void main(String[] args) throws Exception {
String label = "INV-482";
System.out.println((label.startsWith("INV-")) ? "yes" : "no");
}
}
yes
The first four characters are INV-, so this order enters the invoice queue.
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.
class Main {
public static void main(String[] args) throws Exception {
String ticket = "HR-125";
System.out.println(ticket.startsWith("HR-"));
}
}
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.
class Main {
public static void main(String[] args) throws Exception {
String card = "ID:LAB-21";
System.out.println(card.startsWith("LAB-", 3));
}
}
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.