Java StringBuilder chars() method



The Java StringBuilder chars() method is an instance method of the StringBuilder class. It is, used to retrieve a stream of integers representing the zero-indexed char values from the a StringBuilder object. The stream contains the code point values of the characters.

The chars() method does not accept any parameter. It does not throw any exception while retrieving the int stream of the characters in the given sequence.

Note − The stream value is different for lower-case and upper-case characters.

Syntax

Following is the syntax of the Java StringBuilder chars() method −

public IntStream chars()

Parameters

  • It does not accept any parameter.

Return Value

This method returns an IntStream of char values from this sequence.

Example

If the given StringBuilder sequence is not null, the chars() method returns the stream of the character values.

In the following program, we are instantiating the StringBuilder class with the value of TutorialsPoint. Using the chars() method, we are trying to get the stream of the character values of the given sequence.

package com.tutorialspoint.StringBuilder;
import java.util.stream.IntStream;
public class getChars {
   public static void main(String[] args) {
      
      //instantiate the StringBuilder class
      StringBuilder sb = new StringBuilder("TutorialsPoint");
      System.out.println("The string is: " + sb);
      
      //declare intStream variable
      IntStream codepoint = sb.chars();
      System.out.println("The stream of the character values are: ");
      codepoint.forEach(value -> System.out.println(value + " "));
   }
}

Output

On executing the above program, it will produce the following result −

The string is: TutorialsPoint
The stream of the character values are: 
84 
117 
116 
111 
114 
105 
97 
108 
115 
80 
111 
105 
110 
116 

Example

In the following example, we are creating an object of the StringBuilder class with the value of HelloWorld. Then, using the append() method, we are, appending the string java in the given sequence. Using the chars() method, we are, trying to get the stream of the character values.

package com.tutorialspoint.StringBuilder;
import java.util.stream.IntStream;
public class getChars {
   public static void main(String[] args) {
      
      //instantiate the StringBuilder class
      StringBuilder sb = new StringBuilder("HelloWorld ");
      System.out.println("The string is: " + sb);
      
      //using append() method to append value to it.
      sb.append("java");
      System.out.println("After appending the string is: " + sb);
      
      //declare intStream variable
      IntStream codepoint = sb.chars();
      System.out.println("The stream of the character values are: ");
      codepoint.forEach(value -> System.out.print(value + " "));
   }
}

Output

Following is the output of the above program −

The string is: HelloWorld 
After appending the string is: HelloWorld java
The stream of the character values are: 
72 101 108 108 111 87 111 114 108 100 32 106 97 118 97
java_lang_stringbuilder.htm
Advertisements