Definition
Most-used String methods:
- Query: length(), charAt(), contains(), startsWith(), endsWith(), indexOf()
- Compare: equals(), equalsIgnoreCase(), compareTo()
- Transform: substring(), replace(), replaceAll(), toUpperCase(), toLowerCase(), trim(), strip()
- Split/Join: split(), String.join()
- Check: isEmpty(), isBlank() (Java 11+)
String methods are the bank clerk's toolkit: length counts the characters, substring extracts a portion, equals verifies a match, split breaks a CSV line into fields, and trim removes stray spaces from form entries.
Understanding the Concepts
Quick reference with banking examples
String id = " SAV2001 ";
// Trim/Strip
id.trim(); // "SAV2001" (removes ASCII whitespace)
id.strip(); // "SAV2001" (removes Unicode whitespace, Java 11+)
// Length
"SAV2001".length(); // 7
// Substring
"SAV2001".substring(0, 3); // "SAV"
"SAV2001".substring(3); // "2001"
// Search
"SAV2001".contains("2001"); // true
"SAV2001".startsWith("SAV"); // true
"SAV2001".indexOf("2001"); // 3
// Replace
"SAV2001".replace("SAV", "CUR"); // "CUR2001"
// Split
"TX001,TX002,TX003".split(","); // ["TX001", "TX002", "TX003"]
// Case
"savings".toUpperCase(); // "SAVINGS"
// Check
"".isEmpty(); // true
" ".isEmpty(); // false
" ".isBlank(); // true (Java 11+)
What should a senior engineer know?
- strip() vs trim(): strip() handles Unicode whitespace (U+2000 etc.), trim() only handles ASCII ≤ 32.
- split() takes a regex.
"a.b.c".split(".")returns[](empty!) because.matches any char. Usesplit("\\.")orPattern.quote("."). - String.chars() and codePoints() return IntStream for functional processing.
- Java 12+: indent(), transform(). Java 15+: stripIndent(), formatted().
What are common mistakes?
A frequent mistake is using == to compare String content instead of equals().
Wrong approach:
package com.bank.service;
public class AccountValidator {
public boolean validateAccountType(String type) {
// WRONG: == compares references, not content
if (type == "SAVINGS") {
return true;
}
return false;
}
}
Correct approach:
package com.bank.service;
public class AccountValidator {
public boolean validateAccountType(String type) {
// CORRECT: equals() compares content
if ("SAVINGS".equals(type)) { // null-safe: literal first
return true;
}
return false;
}
}
Using the literal on the left side of equals() prevents NullPointerException when the variable is null. This is a standard practice at National Bank of India for all string comparisons in service code.
What are the edge cases?
String behavior has several surprising edge cases that can catch banking developers off guard.
package com.bank.edge;
public class StringEdgeCases {
public static void main(String[] args) {
// Edge case 1: compile-time constant folding
String a = "SAV" + "2001"; // Compile-time constant
String b = "SAV2001";
System.out.println(a == b); // true (same pool entry)
// Edge case 2: variable concatenation is NOT pooled
String prefix = "SAV";
String c = prefix + "2001"; // Runtime concatenation
System.out.println(b == c); // false (different objects)
// Edge case 3: final variable IS a compile-time constant
final String finalPrefix = "SAV";
String d = finalPrefix + "2001"; // Compile-time constant
System.out.println(b == d); // true (same pool entry)
// Edge case 4: substring no longer shares char array (Java 7u6+)
String full = "AccountNumber:SAV2001";
String sub = full.substring(14); // New String, new char array
}
}
true
false
true
The final keyword makes the variable a compile-time constant, which changes how the compiler handles concatenation.
What are common interview follow-up questions?
Interviewers often follow up with these related questions:
- How does this concept apply to a high-throughput banking transaction system?
- What are the performance implications in a production environment?
- How would you debug issues related to this concept?
- What tools would you use to monitor this in production?
- How does this behave differently in Java 8 vs Java 17+?
When answering follow-up questions, always relate your answer back to practical experience. For example, at National Bank of India, Rajesh debugged a production issue related to this concept by using JFR recordings and heap dumps. Providing concrete examples from banking systems shows the interviewer that you understand both the theory and the practice.
Visual Overview
Query: length(), charAt(i), contains(s), startsWith(s), indexOf(s)
Compare: equals(), equalsIgnoreCase(), compareTo()
Transform: substring(), replace(), toUpperCase(), trim(), strip()
Split: split(regex), String.join(delim, parts)
Check: isEmpty(), isBlank() (Java 11+)
All return NEW Strings – original is never modified.
Key Takeaway
The essential String methods are length, substring, equals, contains, replace, split, trim/strip, and isEmpty/isBlank. All transformation methods return new Strings because String is immutable.
Watch out for split() which takes a regex (not a plain string), and use strip() (Java 11+) instead of trim() for Unicode whitespace.
One sentence to remember: String methods return new Strings – query with length/contains/startsWith, transform with substring/replace/toUpperCase, and check with isEmpty/isBlank.
---