GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Strings — java.evolved

Home / Topics / Strings
🏷 Strings 9 patterns

Topic: Strings

All Java patterns related to Strings — java.evolved

Language

Text blocks for multiline strings

Old String json = "{\n" + " \"name\": \"Duke\",\n" + " \"age\": 30\n" + "}";
Modern String json = """ { "name": "Duke", "age": 30 }""";
hover to see modern →
JDK 15+ learn more →
Strings

String.isBlank()

Old boolean blank = str.trim().isEmpty(); // or: str.trim().length() == 0
Modern boolean blank = str.isBlank(); // handles Unicode whitespace too
hover to see modern →
JDK 11+ learn more →
Strings

String.strip() vs trim()

Old // trim() only removes ASCII whitespace // (chars <= U+0020) String clean = str.trim();
Modern // strip() removes all Unicode whitespace String clean = str.strip(); String left = str.stripLeading(); String right = str.stripTrailing();
hover to see modern →
JDK 11+ learn more →
Strings

String.repeat()

Old StringBuilder sb = new StringBuilder(); for (int i = 0; i < 3; i++) { sb.append("abc"); } String result = sb.toString();
Modern String result = "abc".repeat(3); // "abcabcabc"
hover to see modern →
JDK 11+ learn more →
Strings

String.indent() and transform()

Old String[] lines = text.split("\n"); StringBuilder sb = new StringBuilder(); for (String line : lines) { sb.append(" ").append(line) .append("\n"); } String indented = sb.toString();
Modern String indented = text.indent(4); String result = text .transform(String::strip) .transform(s -> s.replace(" ", "-"));
hover to see modern →
JDK 12+ learn more →
Strings

String.formatted()

Old String msg = String.format( "Hello %s, you are %d", name, age );
Modern String msg = "Hello %s, you are %d" .formatted(name, age);
hover to see modern →
JDK 15+ learn more →
Strings

String chars as stream

Old for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (Character.isDigit(c)) { process(c); } }
Modern str.chars() .filter(Character::isDigit) .forEach(c -> process((char) c));
hover to see modern →
JDK 9+ learn more →
Strings

String.lines() for line splitting

Old String text = "one\ntwo\nthree"; String[] lines = text.split("\n"); for (String line : lines) { System.out.println(line); }
Modern String text = "one\ntwo\nthree"; text.lines().forEach(IO::println);
hover to see modern →
JDK 11+ learn more →
Date/Time

HexFormat

Old // Pad to 2 digits, uppercase String hex = String.format( "%02X", byteValue); // Parse hex string int val = Integer.parseInt( "FF", 16);
Modern var hex = HexFormat.of() .withUpperCase(); String s = hex.toHexDigits( byteValue); byte[] bytes = hex.parseHex("48656C6C6F");
hover to see modern →
JDK 17+ learn more →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.