Make a string from an IntStream of code point numbers?
or more direct to String
by using an array passed to new String(…)
IntStream intStream = "input_goes_here".codePoints();
int[] arr;
String output = new String( (arr = intStream.toArray()), 0, arr.length );
and here's the original short solution without the superfluous IntStream intStream
assignment:
int[] arr;
String output = new String( (arr = "input_goes_here".codePoints().toArray()), 0, arr.length );
not to forget the Java IO library:
use IntStream::collect
with a StringWriter
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.
StringWriter::new, // Supplier<R> supplier
StringWriter::write, // ObjIntConsumer<R> accumulator
(w1, w2) -> w1.write( // BiConsumer<R,R> combiner
w2.toString() ) )
.toString();
Use IntStream::collect
with a 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()
;
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 );