Rectangle 27 0

Can one initialise a java String with a single repeated character to a specific length?


String p10Times = Stream.generate(() -> "p").limit(10).collect(Collectors.joining());
String p10times = IntStream.range(0, 10)
  .mapToObj(x -> "p")
  .collect(Collectors.joining());

For the record, with Java8 you can do that with streams:

Note
Rectangle 27 0

Can one initialise a java String with a single repeated character to a specific length?


char[] chars = new char[len];
Arrays.fill(chars, ch);
String s = new String(chars);
Note
Rectangle 27 0

Can one initialise a java String with a single repeated character to a specific length?


StringUtils.repeat(str, count)

Perfect! Why write three lines when you can write 1?

Note
Rectangle 27 0

Can one initialise a java String with a single repeated character to a specific length?


String str = new String(new char[10]).replace("\0", "1");

Here is an elegant and pure Java one-line solution:

Note
Rectangle 27 0

Can one initialise a java String with a single repeated character to a specific length?


StringUtils.repeat(str, count)

Perfect! Why write three lines when you can write 1?

Note