Home > Java > javaTutorial > body text

What new methods have been added to String class in Java 9?

WBOY
Release: 2023-08-20 09:06:28
forward
830 people have browsed it

Java 9中String类添加了哪些新方法?

In Java, String is an immutable class and two new methods were added to the String class in Java 9. These two methods are chars() and codePoints(). Both methods return IntStream objects.

1) chars():

The chars() method of the String class can return an int stream of zero-extended character values ​​from the sequence. The Chinese translation of

Grammar

<strong>public IntStream chars()</strong>
Copy after login

Example

is:

Example

import java.util.stream.IntStream;

public class StringCharsMethodTest {
   public static void main(String args[]) {
      String str = "Welcome to TutorialsPoint";
      IntStream intStream = str.<strong>chars()</strong>;
      intStream.forEach(x -> System.out.printf("-%s", (char)x));
   }
}
Copy after login

Output

<strong>-W-e-l-c-o-m-e- -t-o- -T-u-t-o-r-i-a-l-s-P-o-i-n-t
</strong>
Copy after login

2) codePoints():

The codePoints() method can return a stream of code point values ​​from this sequence. The Chinese translation of

Syntax

<strong>public IntStream codePoints()</strong>
Copy after login

Example

is:

Example

import java.util.stream.IntStream;

public class StringCodePointsMethodTest {
   public static void main(String args[]) {
      String str = "Welcome to Tutorix";
      IntStream intStream = str.<strong>codePoints()</strong>;
      intStream.forEach(x -> System.out.print(new StringBuilder().<strong>appendCodePoint</strong>(x)));
   }
}
Copy after login

Output

<strong>Welcome to Tutorix</strong>
Copy after login

The above is the detailed content of What new methods have been added to String class in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template