Library contents

Java String.contains

Look for a literal sequence anywhere in a label before displaying matching stock.

A simple example

String.contains

  1. Start with "abc", looking for "b"
  2. Do this Search inside the text
  3. Result Found
The middle character matches the text being sought.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println("abc".contains("b"));
    }
}
Output
true

Syntax

Java syntax
label.contains("tea")
JDK 21 method signatures (1)
public boolean contains(CharSequence s)
Run these examples in Java

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

java Main.java

Find a word on a shelf label

A stockroom worker searches a shelf description for a particular word. Inspect the match result before using it to highlight the relevant part of the shelf label.

From the programString label = "green tea bags"
Resultyes
Main.java

class Main {
    public static void main(String[] args) throws Exception {
        String label = "green tea bags";
        System.out.println((label.contains("tea")) ? "yes" : "no");
    }
}
Output
yes

The letters t-e-a occur consecutively in the label. The check would also match teapot.

Advertisements

Find an item in a lunch description

A search box accepts a short piece of text and looks for it inside a longer description. Check where the sequence occurs before deciding whether the product or shelf label belongs in the results.

From the programString lunch = "lemon rice"
Resulttrue
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String lunch = "lemon rice";
        System.out.println(lunch.contains("rice"));
    }
}
Output
true

The last four letters match the search term.

Check a literal dot in a filename

A filename filter looks for an actual period between a name and its extension. Treat the period as a literal character so an unrelated filename cannot match merely because it contains some character.

From the programString name = "readme"
Resultfalse
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String name = "readme";
        System.out.println(name.contains("."));
    }
}
Output
false

The dot is a literal character, so it cannot match arbitrary text.

Route a lift fault to the right team

A caretaker receives a ticket with the text “East lift stalled on floor 2”. Tickets mentioning a lift go to the lift crew. The incoming system uses lower-case equipment names consistently.

GivenEast lift stalled on floor 2
ResultLift crew
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String ticket = "East lift stalled on floor 2";
        System.out.println(ticket.contains("lift") ? "Lift crew" : "General repairs");
    }
}
Output
Lift crew

The word lift occurs inside the ticket text, so the ticket is routed to the lift crew. A search for a literal substring also matches inside longer words.

What to watch for

For whole-word searching, specify word boundaries rather than a plain substring.

Documentation for String.contains