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

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

WBOY
WBOYforward
2023-08-20 09:06:28819browse

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>

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));
   }
}

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>

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>

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)));
   }
}

Output

<strong>Welcome to Tutorix</strong>

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete