String Search Methods: contains(), startsWith(), endsWith()
Fast recall
Quick revision
Core answer
contains(s) returns true if the substring s appears anywhere in the string. startsWith(prefix) checks if the string begins with the given prefix. endsWith(suffix) checks if it ends with the given suffix. All three return boolean and are case-sensitive.Whether you know the basic String search API and understand that all three are case-sensitive.
Forgetting that these are all case-sensitive — 'Hello'.contains('hello') returns false.
Mention indexOf() returns the position (-1 if not found), which is useful when you need to know WHERE in the string something occurs, not just IF. Also mention that contains() is implemented using indexOf() internally.
Interview-ready guide
Interview answer
These are the three everyday substring checks, and they all hand back a simple boolean. contains() asks whether a substring appears anywhere; startsWith() checks the front; endsWith() checks the end — handy for things like name.endsWith(".pdf").
The one mistake almost everyone makes is forgetting they are case-sensitive. "Hello".contains("hello") returns false, and there is no case-insensitive version built in. When case should not matter, I lowercase both sides — text.toLowerCase().contains(term.toLowerCase()) — and the both part matters, because normalising only one side still misses.
The detail I would add to show I know the API is that contains() is really just indexOf() underneath: indexOf(s) returns the position of the first match, or -1 if it is not there, and contains() simply checks whether that is not -1. So they cost the same. The practical takeaway is to pick based on what you need back — if I only want a yes/no, contains() reads cleanest; if I need to know where the match is so I can slice the String, I go straight to indexOf().
String Search Methods: contains(), startsWith(), endsWith()
Direct Answer
contains(s) returns true if the substring s appears anywhere in the string. startsWith(prefix) checks if the string begins with the given prefix. endsWith(suffix) checks if it ends with the given suffix. All three return boolean and are case-sensitive.
Code Example
public class StringSearchDemo {
public static void main(String[] args) {
String url = "https://www.example.com/products";
// contains() — is the substring anywhere in the string?
System.out.println(url.contains("example")); // true
System.out.println(url.contains("EXAMPLE")); // false — case-sensitive!
// startsWith() — does it begin with this?
System.out.println(url.startsWith("https")); // true
System.out.println(url.startsWith("http://")); // false
// endsWith() — does it end with this?
System.out.println(url.endsWith("/products")); // true
System.out.println(url.endsWith(".html")); // false
// indexOf() — WHERE is the match? Returns -1 if not found
System.out.println(url.indexOf("example")); // 12 — start index
System.out.println(url.indexOf("EXAMPLE")); // -1 — case-sensitive, not found
// Case-insensitive search — normalise both sides
String search = "EXAMPLE";
System.out.println(url.toLowerCase().contains(search.toLowerCase())); // true
}
}
Common Mistakes & Pitfalls
- Forgetting they are case-sensitive —
"Hello".contains("hello")isfalse; normalise withtoLowerCase()when case should not matter. - Normalising only one side —
text.toLowerCase().contains(term)still misses whentermhas capitals; lowercase both sides. - Using
contains()when you need the position — it only returns yes/no; reach forindexOf()when you have to slice or locate the match. - Treating
indexOfreturning0as not found —0means the match is at the very start; only-1means absent.
Check your understanding
Answer these without looking back at the article.
- How do you search for a substring case-insensitively in Java?
- What does String.matches() do and how is it different from contains()?
- What is the difference between indexOf() and contains()?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.