Pages

Thursday, July 16, 2020

Java String codePoints() Example

1. Overview

Java String codePoints()

In this String API Series, You'll learn how to convert String to IntStream with codepoints.

In the new version of java 9, the String class is added with the codePoints() method and returns Stream with integer values.

codePoints() method returns a stream of code point values from this sequence. Any surrogate pairs encountered in the sequence are combined as if by Character.toCodePoint and the result is passed to the stream. Any other code units, including ordinary BMP characters, unpaired surrogates, and undefined code units, are zero-extended to int values which are then passed to the stream.

Java String codePoints()

2. codePoints() Syntax


Syntax:
public IntStream codePoints()
This method takes no method arguments and returns an IntStream of Unicode code points from this sequence codePoints() method does not throw any exception at runtime. But, if you call this method on null string reference then NullPointerException is thrown.

3. Java String codePoints() Example To Convert String to IntStream


package com.javaprogramto.strings;

import java.util.stream.IntStream;

public class StringCodepoinExampleToConvertStream {

    public static void main(String[] args) {

        String str = "Code Points as Stream";

        System.out.println("Input string value : "+str);

        IntStream intStream = str.codePoints();

        System.out.println("Printing each char from string as ASCII value");
        intStream.forEach(value -> System.out.print(value+" "));

    }
}

Output:

Input string value : Code Points as Stream
Printing each char from string as ASCII value
67 111 100 101 32 80 111 105 110 116 115 32 97 115 32 83 116 114 101 97 109 

4. codePoints() Example To Remove ZERO's from String


package com.javaprogramto.strings;

import java.util.stream.IntStream;

public class StringCodepoinExampleToRemoveChar {

    public static void main(String[] args) {

        String str = "Digit ZERO 0 is not considered in input name. So removing all Zero's 00000000";

        System.out.println("Input string value : " + str);

        IntStream intStream = str.codePoints();

        String zeroRemovedString = intStream.filter(ch -> ch != 48)
                .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
                .toString();

        System.out.println("Zero's are removed from input. Final output : "+zeroRemovedString);

    }
}

Output:

Input string value : Digit ZERO 0 is not considered in input name. So removing all Zero's 00000000
Zero's are removed from input. Final output : Digit ZERO  is not considered in input name. So removing all Zero's 

5. Conclusion


In this article, You've seen how to convert String to IntStream.

And also a few examples to get all codepoints of String and how to remove a specific character from a string using Stream API.

As usual, all examples are on GitHub.


Read more examples on Code Points Concept.




No comments:

Post a Comment

Please do not add any spam links in the comments section.