Search in a String: indexOf, contains, startsWith
20 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Java can search inside a String for you. indexOf() tells you where some text is, and contains(), startsWith() and endsWith() tell you whether it is there.
Where is it? indexOf()
To split an email later, an app must first know where the @ is. indexOf(x) gives the index of the first x, a character or a word, and -1 when x is not there.
Example 1 · Find positions in an email
public class EmailPositions { public static void main(String[] args) { String email = "ravi.k@gmail.com"; System.out.println("@ at index " + email.indexOf('@')); System.out.println("gmail starts at " + email.indexOf("gmail")); System.out.println("# at index " + email.indexOf('#')); }}@ at index 6 gmail starts at 7 # at index -1
What this code does
indexOf('@')searches for onechar(single quotes);indexOf("gmail")searches for a word and gives the index of its first letter.- There is no
#in the email, soindexOf('#')gives -1.
No real index is -1, so if (at == -1) means "not found". Check it before you use the number as an index.
The last match, or the next one
In "notes.final.pdf" the file type starts after the last dot, not the first. lastIndexOf(x) searches from the end; indexOf(x, from) starts its search at index from.
Example 2 · The last dot and the next dot
public class FileDots { public static void main(String[] args) { String file = "notes.final.pdf"; int first = file.indexOf('.'); System.out.println("First dot: " + first); System.out.println("Last dot: " + file.lastIndexOf('.')); System.out.println("Next dot: " + file.indexOf('.', first + 1)); }}First dot: 5 Last dot: 11 Next dot: 11
What this code does
lastIndexOf('.')gives 11, the dot just beforepdf.indexOf('.', first + 1)starts one place after the first dot, so it skips it and finds the next one.- Here the next dot is also the last one, so both give 11.
Yes or no: contains(), startsWith(), endsWith()
A sign-up form must reject an email without @ and a phone number that does not start with +91. contains(), startsWith() and endsWith() answer with a boolean, so they go straight into an if.
Example 3 · A simple email check
public class EmailCheck { public static void main(String[] args) { String email = "anu.r@college.edu.in"; boolean hasAt = email.contains("@"); boolean hasSpace = email.contains(" "); if (hasAt && !hasSpace && !email.startsWith("@")) { System.out.println("Looks like an email"); } else { System.out.println("Not a valid email"); } }}Looks like an email
What this code does
- Each check gives
trueorfalse, so they join with&&and!like any condition (Logical operators (opens in a new tab)). contains()needs a String, so it iscontains("@")with double quotes.
This check is basic on purpose: it catches common typing mistakes, like a missing @, not every wrong email.
Classify
Which method answers it?
Tap a question, then tap the method that answers it.
Capital letters matter
A student uploads Resume_Final.PDF and the check endsWith(".pdf") says no. All these searches are case-sensitive: capital and small letters are different characters.
Example 4 · Capital letters matter
public class CaseCheck { public static void main(String[] args) { String phone = "+919876543210"; String file = "Resume_Final.PDF"; System.out.println(phone.startsWith("+91")); System.out.println(file.endsWith(".pdf")); // false System.out.println(file.endsWith(".PDF")); }}true false true
What this code does
.pdfand.PDFare different text, so the second check isfalse.- The same rule holds for
indexOf()andcontains():"Ravi".contains("ravi")isfalse.
toLowerCase(), which fixes this kind of check.Next step
Check your understanding
9 quick questions