Java String chars() Method Tutorial

In this section we will learn what the String chars() method is and how to use it in Java.

What is Java String chars() Method?

The Java String chars() method is used to return a stream of code points for characters in a string value.

In short, the chars() method is used to convert a string value into a stream.

Java chars() Method Syntax:

public IntStream chars()

chars() Method Parameters:

The method does not take an argument.

chars() Method Return Value:

The return value of this method is a stream of type IntStream.

chars() Method Exceptions:

The method does not throw an exception.

Example: using String chars() method

import java.util.stream.IntStream;

public class Main {

   public static void main(String[] args) {

      IntStream stream = "world".chars();
      stream.forEach(i -> System.out.println((char)i));

   }
}

Output:

w
o
r
l
d

Leave a Reply