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
In addition to the answers above, you should initialize the
StringBuilder
with an appropriate capacity, especially that you already know it. For example:In case of Java 8 you can do:
Output:
In Java 8 the
String.join
method was added. ButCollections.nCopies
is even in Java 5.For the case of repeating a single character (not a String), you could use Arrays.fill: