Last 30 Days
No notifications
String is the most-used class in Java. Two crucial facts:
1. Strings are immutable. Every "modification" returns a new String.
2. Compare with .equals() ā never ==.
String name = "Asha";int len = name.length(); // 4
char first = name.charAt(0); // 'A'
String upper = name.toUpperCase(); // "ASHA" (new string)
String slice = name.substring(1, 3); // "sh"
boolean has = name.contains("sh"); // true
String rep = name.replace("a", "@"); // "@sh@"
String trim = " hi ".strip(); // "hi" (Java 11+)
StringBuilderStringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++) {
sb.append(i).append(',');
}
String result = sb.toString();Concatenating strings in a loop with + is O(n²). StringBuilder is O(n).
String html = """
<h1>Hello</h1>
<p>Multi-line, no escaping.</p>
""";String s1 = String.format("name=%s age=%d", "Asha", 19);
String s2 = "name=%s age=%d".formatted("Asha", 19); // Java 15+Once a String is built, you can never change its characters. s.toUpperCase() doesn't modify s ā it returns a brand-new String.
hashCode() is cached on first call.String s = "hello";
s.toUpperCase(); // returns "HELLO" but discarded
System.out.println(s); // still "hello"s = s.toUpperCase(); // assign back to keep
.equals()String a = "hi";
String b = "hi";
String c = new String("hi");a == b // true (both interned literals ā same object)
a == c // false (new String makes a new object)
a.equals(c) // true (same content)
a.equalsIgnoreCase("HI") // true
> Use Objects.equals(x, y) if either side might be null ā it handles null == null for you.
String s = "Hello, Java!";s.length(); // 12
s.charAt(7); // 'J'
s.indexOf("Java"); // 7
s.indexOf('z'); // -1 (not found)
s.lastIndexOf('l'); // 3
s.substring(7); // "Java!"
s.substring(7, 11); // "Java"
s.toLowerCase(); // "hello, java!"
s.toUpperCase(); // "HELLO, JAVA!"
s.replace('l', 'L'); // "HeLLo, Java!"
s.replace("Java", "World"); // "Hello, World!"
s.contains("Java"); // true
s.startsWith("Hello"); // true
s.endsWith("!"); // true
s.trim(); // strips ASCII whitespace
s.strip(); // strips Unicode whitespace (Java 11+)
s.isEmpty(); // false (length == 0)
s.isBlank(); // false (only whitespace) (Java 11+)
String csv = "a,b,c,d";
String[] parts = csv.split(","); // {"a","b","c","d"}// limit: stop after N tokens
"a,b,c,d".split(",", 2); // {"a", "b,c,d"}
// regex split
"one two three".split("\\s+"); // {"one","two","three"}
// join
String joined = String.join("
", "a", "b", "c"); // "a b
c"
String fromList = String.join(",", List.of("x","y")); // "x,y"> split takes a regex, not a literal. Use Pattern.quote(".") or split("\\.") to split on a literal period.
String ā char[]char[] chars = "hi".toCharArray(); // {'h', 'i'}
String back = new String(chars); // "hi"
String back2 = String.valueOf(chars); // "hi""apple".compareTo("banana"); // negative (a < b)
"apple".compareToIgnoreCase("APPLE"); // 0List<String> names = new ArrayList<>(List.of("Mia", "Asha", "Bishan"));
Collections.sort(names); // alphabetical
names.sort(String.CASE_INSENSITIVE_ORDER);
StringBuilder ā When You Need SpeedStrings are immutable, so:
String s = "";
for (int i = 0; i < 10_000; i++) {
s = s + i; // creates 10,000 new strings ā O(n²)
}Use StringBuilder instead:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10_000; i++) {
sb.append(i); // mutates in place ā O(n)
}
String result = sb.toString();StringBuilder APIStringBuilder sb = new StringBuilder("Hello");
sb.append(", ").append("world").append('!'); // chainable
sb.insert(0, ">>> "); // ">>> Hello, world!"
sb.delete(0, 4); // "Hello, world!"
sb.deleteCharAt(0); // "ello, world!"
sb.reverse(); // "!dlrow ,olle"
sb.length();
sb.toString();> Need a thread-safe variant? Use StringBuffer (synchronized, slower). 99% of the time StringBuilder is the right pick.
String json = """
{
"name": "Asha",
"age": 19
}
""";String sql = """
SELECT id, name
FROM users
WHERE active = true
""";
\n, \t work; " doesn't need escaping.\ joins to next line without newline.String.format("%-10s %5d", "Asha", 19); // "Asha 19"
"%-10s %5d".formatted("Asha", 19); // Java 15+ instance method
String.format("%.3f", Math.PI); // "3.142""hello123".matches("[a-z]+\\d+"); // true
"a-b-c".replaceAll("-", "/"); // "a/b/c"
"a1b22c".split("\\d+"); // {"a","b","c"}For complex regex, use Pattern + Matcher directly.
String ā Numberint n = Integer.parseInt("42");
double d = Double.parseDouble("3.14");
String s = String.valueOf(42); // "42"
String b = Integer.toString(42, 2); // "101010" (radix)Integer.parseInt throws NumberFormatException on bad input ā wrap in try/catch or validate first.
"hi" literals are the SAME object.new String("hi") forces a new object ā almost never what you want.StringBuilder in loops; modern javac already converts simple a + b + c (no loop) to StringBuilder calls automatically.String.format sparingly ā it's slower than concat.| Need | Code |
| Length | s.length() |
| Get char | s.charAt(i) |
| Substring | s.substring(a, b) |
| Split | s.split(regex) |
| Join | String.join(",", parts) |
| Equality | a.equals(b) |
| Case-insensitive eq | a.equalsIgnoreCase(b) |
| Trim | s.strip() (Java 11+) |
| Contains | s.contains(sub) |
| Replace | s.replace(old, new) |
| Format | String.format(fmt, ...) or fmt.formatted(...) |
| Build in loop | StringBuilder |
| Multi-line | text block """ |
You can now wrangle text efficiently. Next: arrays ā Java's fixed-size containers.