beginnerFree
String Methods
Deep dive - trim, replace, split & more

String is immutable - the original is never modified. Transform methods (trim/replace) return new strings; query methods return booleans; split returns an array.
Watch
The narrated version of this skribble — tap play for the full-size video.
0:00 / 2:25
Deep dive
The Big Picture
String methods transform, search, and extract text. Strings are immutable: methods never change the receiver.
length(),charAt(),substring()- basicscontains(),indexOf(),startsWith()- searchingtoUpperCase(),trim(),replace()- transforms
String has 60+ methods - learn the top 10, look up the rest.
How It Works
Simple methods are O(n) or better; regex methods cost more.
substring()creates a new String (since Java 7u6)split()uses regex - can be slow for simple delimitersmatches()compiles regex each call - cachePatternjoin()for delimited text; repeated assembly useStringBuilderchars()returnsIntStreamfor functional processing
In the Java Landscape
String methods grew with each Java version.
- Java 8:
String.join()for joining with delimiter - Java 11:
strip()(Unicode-aware),isBlank(),repeat() - Java 12:
indent(),transform() - Apache
StringUtilsfills gaps: null-safe, padding, abbreviation - Regex:
PatternandMatcherfor complex text processing
Pitfalls and Best Practices
- Use
strip()nottrim()(handles Unicode whitespace) equalsIgnoreCase()for case-insensitive comparisonisEmpty()checks length 0;isBlank()checks whitespace only- Chain carefully - null at any point throws NPE
- Cache compiled
Patternobjects for repeated regex use - Use
replace()for literal,replaceAll()for regex



