Lesson Completion
Back to course

String Searching & Extraction: Finding and Extracting Text

Beginner
15 minutes4.7Java

1. The Hook (The "Byte-Sized" Intro)

In a Nutshell: Java provides methods to find text (indexOf, contains), check boundaries (startsWith, endsWith), and extract portions (substring, split).

When Gmail searches emails: email.contains("urgent") filters your inbox. Finding text is fundamental!


2. Quick Reference

MethodReturnsPurpose
indexOf(str)intFirst position of str (-1 if not found)
lastIndexOf(str)intLast position of str
contains(str)booleanWhether str exists
startsWith(str)booleanBegins with str?
endsWith(str)booleanEnds with str?
substring(start)StringFrom start to end
substring(start, end)StringFrom start to end-1
split(regex)String[]Split by delimiter

3. Interactive & Applied Code

java
public class StringSearch { public static void main(String[] args) { String text = "Hello, World! Welcome to Java!"; // === FINDING === System.out.println(text.indexOf("o")); // 4 (first 'o') System.out.println(text.lastIndexOf("o")); // 25 (last 'o') System.out.println(text.indexOf("xyz")); // -1 (not found) System.out.println(text.indexOf("o", 5)); // 8 (from index 5) // === CHECKING === System.out.println(text.contains("World")); // true System.out.println(text.startsWith("Hello")); // true System.out.println(text.endsWith("!")); // true // === EXTRACTING === System.out.println(text.substring(7)); // "World! Welcome to Java!" System.out.println(text.substring(7, 12)); // "World" (7 to 11 inclusive) // === SPLITTING === String csv = "apple,banana,cherry"; String[] fruits = csv.split(","); // ["apple", "banana", "cherry"] String sentence = "Hello World"; String[] words = sentence.split("\\s+"); // Regex: one or more spaces // ["Hello", "World"] // Split with limit String data = "a:b:c:d"; String[] parts = data.split(":", 2); // Max 2 parts // ["a", "b:c:d"] // === REAL-WORLD: Email parsing === String email = "user@example.com"; String username = email.substring(0, email.indexOf("@")); String domain = email.substring(email.indexOf("@") + 1); System.out.println("User: " + username); // user System.out.println("Domain: " + domain); // example.com // === REAL-WORLD: File extension === String filename = "document.pdf"; String extension = filename.substring(filename.lastIndexOf(".") + 1); System.out.println("Extension: " + extension); // pdf } }

⚠️ Common Mistakes

Mistake #1: Not checking indexOf result

java
String s = "hello"; String sub = s.substring(s.indexOf("x")); // ❌ indexOf returns -1! // Check first: if (s.indexOf("x") != -1)

Mistake #2: substring end is exclusive

java
"hello".substring(0, 5); // "hello" (indices 0-4) "hello".substring(0, 6); // ❌ StringIndexOutOfBoundsException!

4. The "Interview Corner"

🏆 Q1: "What's the time complexity of indexOf()?" Answer: O(n*m) where n = string length, m = search length. Uses naive algorithm (not KMP).

🏆 Q2: "Difference between contains() and indexOf()?" Answer: contains() returns boolean, indexOf() returns position. Internally, contains() calls indexOf() >= 0.


🎓 Key Takeaways

indexOf returns position or -1
substring(start, end) is exclusive of end
split() accepts regex patterns
✅ Always check indexOf result before using

Topics Covered

Java FundamentalsStrings

Tags

#java#strings#string-manipulation#string-methods#beginner-friendly

Last Updated

2025-02-01