Is there a simple way to add a character or another String n-times to an existing String?
I couldn’t find anything in String
, Stringbuilder
, etc.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
To have an idea of the speed penalty, I have tested two versions, one with Array.fill and one with StringBuilder.
and
using
(note the loop in main function is to kick in JIT)
The results are as follows:
It is a huge difference
You can use Guava's Strings.repeat method:
Here is a simple way..
You are able to do this using Java 8 stream APIs. The following code creates the string
"cccc"
from"c"
:Use this: