0

I'm studying Java recently.

Is there any way to make a string from char with N-times?

C#: string str = new string('A', 30);

Java: ???

please help me. Thank you

  • 5
    Check this: https://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string-in-java – 0xh3xa Mar 26 '20 at 12:07
  • 3
    Does this answer your question? [Simple way to repeat a String in java](https://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string-in-java) – B. Go Mar 26 '20 at 13:55

2 Answers2

3

What version of java are you using?

If Java 8

String.join("", Collections.nCopies(n, s));

If Java 11

"abc".repeat(12);
nz_21
  • 6,140
  • 7
  • 34
  • 80
0

This is one way you could do it using Java 8 Streams.

        String aNTimes = IntStream.range(0, 30)
                .mapToObj(index -> "A")
                .collect(Collectors.joining());
Jason
  • 5,154
  • 2
  • 12
  • 22