Skip to main content

String Manipulation

Learn String Manipulation (Strings, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Java Strings Are Immutable Every "modification" of a String - concatenation, substring, replace - creates a new String object; the original is never changed. This is why building a string inside a loop with += is O(n²): each concatenation allocates a whole new copy. Use StringBuilder for repeated modifications - it's mutable and appends in amortized O(1). charAt(i) , substring(l, r) , toCharArray() are the core building blocks for most string algorithms.

More lessons in Strings