Make a string from an IntStream of code point numbers?(从代码点编号的 IntStream 中创建一个字符串?)
问题描述
如果我正在使用 Java 流,并以 IntStream of Unicode 字符的>code point 数字,如何呈现 CharSequence 比如 String?
If I am working with Java streams, and end up with an IntStream of code point numbers for Unicode characters, how can I render a CharSequence such as a String?
String output = "input_goes_here".codePoints(). ??? ;
我在几个接口上找到了一个 codePoints() 方法 &所有生成代码点的 IntStream 的类.但是我还没有找到任何可以接受相同的构造函数或工厂方法.
I have found a codePoints() method on several interfaces & classes that all generate an IntStream of code points. Yet I have not been able to find any constructor or factory method that accepts the same.
CharSequence::codePoints() → IntStreamString::codePoints() → IntStreamStringBuilder::codePoints() → IntStream
我正在寻找相反的:
➥ 如何从 IntStream 的代码点实例化 String 或 CharSequence 等?
➥ How to instantiate a String or CharSequence or such from an IntStream of code points?
推荐答案
使用IntStream::collect 带有 StringBuilder.
String output =
"input_goes_here"
.codePoints() // Generates an `IntStream` of Unicode code points, one `Integer` for each character in the string.
.collect( // Collect the results of processing each code point.
StringBuilder::new, // Supplier<R> supplier
StringBuilder::appendCodePoint, // ObjIntConsumer<R> accumulator
StringBuilder::append // BiConsumer<R,R> combiner
)
.toString()
;
如果您喜欢更通用的 CharSequence 接口在具体 String,只需将 toString() 放在末尾即可.返回的 StringBuilder 是一个 CharSequence.
If you prefer the more general CharSequence interface over concrete String, simply drop the toString() at the end. The returned StringBuilder is a CharSequence.
IntStream codePointStream = "input_goes_here".codePoints ();
CharSequence output = codePointStream.collect ( StringBuilder :: new , StringBuilder :: appendCodePoint , StringBuilder :: append );
这篇关于从代码点编号的 IntStream 中创建一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从代码点编号的 IntStream 中创建一个字符串?
基础教程推荐
- 多个组件的复杂布局 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
