String Methods
String manipulation and formatting
Introduction
In this lesson, you'll learn about string methods in Java. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In JavaScript, you're familiar with string manipulation and formatting.
Java has its own approach to string manipulation and formatting, which we'll explore step by step.
The Java Way
Let's see how Java handles this concept. Here's a typical example:
String s = "Hello, World!";
s.length(); // 13 — note: method, not property
s.toUpperCase(); // "HELLO, WORLD!"
s.toLowerCase(); // "hello, world!"
s.contains("World"); // true
s.startsWith("Hello"); // true
s.indexOf("o"); // 4
s.substring(7, 12); // "World"
s.replace("World","Java"); // "Hello, Java!"
s.split(", "); // String[]{"Hello","World!"}
s.strip(); // removes whitespace (Java 11+)
// String.format (printf-style)
String name = "Alice";
String.format("Hello, %s!", name); // "Hello, Alice!"
// Java 15+ text blocks
String json = """
{"name": "Alice"}
""";
// Padding (Java 11+)
"5".repeat(3); // "555"
String.format("%3s","5").replace(' ','0'); // "005"Comparing to JavaScript
Here's how you might have written similar code in JavaScript:
const s = "Hello, World!";
s.length; // 13
s.toUpperCase(); // "HELLO, WORLD!"
s.toLowerCase(); // "hello, world!"
s.includes("World"); // true
s.startsWith("Hello"); // true
s.indexOf("o"); // 4
s.slice(7, 12); // "World"
s.replace("World","JS");// "Hello, JS!"
s.split(", "); // ["Hello", "World!"]
s.trim(); // removes whitespace
// Template literals
const name = "Alice";
`Hello, ${name}!`; // "Hello, Alice!"
// Padding
"5".padStart(3, "0"); // "005"You may be used to different syntax or behavior.
length is a method length() in Java, not a property
You may be used to different syntax or behavior.
substring(start, end) vs JS slice(start, end) — same semantics
You may be used to different syntax or behavior.
contains() vs JS includes(); indexOf() works the same
You may be used to different syntax or behavior.
String.format() uses printf-style %s/%d/%f placeholders
You may be used to different syntax or behavior.
Java strings are immutable objects; all methods return new strings
Step-by-Step Breakdown
1. length() is a Method
In Java, String.length() is a method call, not a property access. Arrays use .length (no parens), but String uses .length().
s.lengths.length() // String
arr.length // array (no parens)2. Substrings
substring(start, end) returns [start, end) — same as JS slice. But JS also supports negative indices; Java does not.
s.slice(7, 12)s.substring(7, 12)3. String.format
Use String.format() for formatted strings with %s (string), %d (integer), %f (float), %.2f (2 decimal places).
`Balance: ${balance.toFixed(2)}`String.format("Balance: %.2f", balance)4. String Comparison
Never use == to compare Java strings — it compares references, not content. Always use .equals() or .equalsIgnoreCase().
s1 === s2s1.equals(s2)
s1.equalsIgnoreCase(s2)Common Mistakes
When coming from JavaScript, developers often make these mistakes:
- length is a method length() in Java, not a property
- substring(start, end) vs JS slice(start, end) — same semantics
- contains() vs JS includes(); indexOf() works the same
Key Takeaways
- length() is a method; substring(), contains(), replace() all return new strings
- String.format("%s %d %.2f", ...) for printf-style formatting
- Always use .equals() for string comparison, never ==
- Java 15+ text blocks (triple-quote) replace messy string concatenation