String API — Deep Manipulation
The String class has 60+ methods covering search, extraction, transformation, comparison, and formatting. Knowing which method to reach for saves time and produces readable, efficient code. String is immutable — every mutating operation returns a new String.
Essential String Methods
public class StringMethods {
public static void main(String[] args) {
String title = " effective java ";
// Cleaning
System.out.println(title.strip()); // "effective java" (Java 11, Unicode-aware)
System.out.println(title.stripLeading()); // "effective java "
System.out.println(title.stripTrailing()); // " effective java"
System.out.println("".isBlank()); // true (Java 11)
System.out.println("hello".repeat(3)); // "hellohellohello" (Java 11)
// Search & test
String book = "The Quick Brown Fox";
System.out.println(book.contains("Quick")); // true
System.out.println(book.startsWith("The")); // true
System.out.println(book.endsWith("Fox")); // true
System.out.println(book.indexOf("Quick")); // 4
System.out.println(book.lastIndexOf("o")); // 17
// Extraction
System.out.println(book.substring(4)); // "Quick Brown Fox"
System.out.println(book.substring(4, 9)); // "Quick"
// Transformation (returns NEW String — original unchanged)
System.out.println(book.toLowerCase()); // "the quick brown fox"
System.out.println(book.replace("Fox", "Cat")); // "The Quick Brown Cat"
System.out.println(book.replaceAll("\s+", "-")); // "The-Quick-Brown-Fox" (regex)
// Split & join
String csv = "Java,Python,Go,Rust";
String[] langs = csv.split(","); // ["Java","Python","Go","Rust"]
System.out.println(String.join(" | ", langs)); // "Java | Python | Go | Rust"
System.out.println(String.join(", ", "A", "B", "C")); // varargs form
// Comparison
System.out.println("Java".equals("java")); // false
System.out.println("Java".equalsIgnoreCase("java")); // true
System.out.println("apple".compareTo("banana")); // negative (a before b)
// Chars & conversion
System.out.println(book.charAt(4)); // 'Q'
System.out.println(book.toCharArray().length); // 19
System.out.println(String.valueOf(42)); // "42"
System.out.println(Integer.parseInt("42") + 1); // 43
}
}Quick Quiz
Tip
Tip
Practice String API Deep Manipulation in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
JVM enables 'Write Once, Run Anywhere' — bytecode runs on any platform
Practice Task
Note
Practice Task — (1) Write a working example of String API Deep Manipulation from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with String API Deep Manipulation is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.