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 — String chars as stream | java.evolved

Home / Strings / String chars as stream
Strings Beginner

String chars as stream

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Process string characters as a stream pipeline.

Strings Streams API
Code Comparison
✕ Java 8
for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
    if (Character.isDigit(c)) {
        process(c);
    }
}
✓ Java 9+
str.chars()
    .filter(Character::isDigit)
    .forEach(c -> process((char) c));
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
🔗

Chainable

Use filter, map, collect on character streams.

📐

Declarative

Describe what to do, not how to loop.

🌐

Unicode-ready

codePoints() correctly handles emoji and supplementary chars.

Old Approach
Manual Loop
Modern Approach
chars() Stream
Since JDK
9
Difficulty
Beginner
JDK Support
String chars as stream
Available

Available since JDK 8+ (improved in 9+)

How it works

String.chars() returns an IntStream of character values, enabling functional processing. For Unicode support, codePoints() handles supplementary characters correctly.

Related Documentation
String.chars() ↗ CharSequence.codePoints() ↗
Proof
View proof source ↗
Related patterns
Strings Beginner

String.repeat()

Java 8
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 3; i++) {
    sb.append("abc");
}
String result = sb.toString();
Java 11+
String result = "abc".repeat(3);
// "abcabcabc"
Hover to see modern ➜
JDK 11+ →
Strings Beginner

String.isBlank()

Java 8
boolean blank =
    str.trim().isEmpty();
// or: str.trim().length() == 0
Java 11+
boolean blank = str.isBlank();
// handles Unicode whitespace too
Hover to see modern ➜
JDK 11+ →
Strings Beginner

String.formatted()

Java 8
String msg = String.format(
    "Hello %s, you are %d",
    name, age
);
Java 15+
String msg =
    "Hello %s, you are %d"
    .formatted(name, age);
Hover to see modern ➜
JDK 15+ →
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.