Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

Java String.chars() method

Reference for Java String.chars() and codePoints(): turn a string into an IntStream to count, filter and transform characters, and handle emoji correctly.

chars() returns an IntStream of the UTF-16 code units in a string. Each element is a char value widened to int, which is why you often see a cast back to char in stream pipelines. It lets you use filter, map, count and collect-style processing on text without writing a loop.

A sibling method, codePoints(), returns real Unicode code points instead. The two differ for characters outside the Basic Multilingual Plane (most emoji), which take two char values in Java.

Java
long digits = "a1b22".chars().filter(Character::isDigit).count(); // 3

Try it

Java Counting and transforming characters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java chars() versus codePoints()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public IntStream chars()          // since 9 on String (declared by CharSequence since 8)
public IntStream codePoints()     // since 9 on String (declared by CharSequence since 8)

Returns: an IntStream in string order. chars() yields length() elements, codePoints() yields one element per code point, with each valid surrogate pair combined; an unpaired surrogate is passed through as is.

Throws: nothing. The stream is sequential, ordered, immutable in content and sized, so count() can answer without traversing in many cases.

Usage notes

  • Elements are int, not char. mapToObj(c -> (char) c) gives a Stream<Character>; mapToObj(c -> String.valueOf((char) c)) gives a Stream<String>.
  • Prefer codePoints() for user text. With chars(), an emoji counts as two elements and splitting or reversing it breaks the character apart.
  • Primitive stream operations: sum(), max(), average(), filter(), map(), distinct(), sorted() and boxed() are available. There is no collect(Collectors...) directly; call boxed() or mapToObj() first.
  • Rebuild a string with mapToObj plus Collectors.joining(), or with StringBuilder::appendCodePoint through collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).
  • Performance. A plain for loop over charAt() is faster and allocates nothing; use chars() when clarity matters more than the last few nanoseconds.
  • The string is not modified. All results are new values.
  • Character classification methods such as Character.isLetter(int) have int overloads, so they work as method references on these streams.