I have a stream of characters like
"abcd".chars()
I am using peek to print out stream like this
"abcd".chars().peek(e->System.out.print(e + ":"))
The only problem is that it print out as
a:b:c:d:
I would like to replace the last colon with newline, but don't know how to do that, anyone could help?
Using the joining
collector is another option:
String result = Arrays.stream(string.split(""))
.collect(Collectors.joining(":"));
or:
String result = Pattern.compile("")
.splitAsStream(string)
.collect(Collectors.joining(":"));
if you actually want a new line then use this overload of the joining
collector
Collectors.joining(":","","\n")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With