Create a string with n characters

前端 未结 27 1115
猫巷女王i
猫巷女王i 2020-11-28 22:12

Is there a way in java to create a string with a specified number of a specified character? In my case, I would need to create a string with 10 spaces. My current code is:

27条回答
  •  清酒与你
    2020-11-28 22:56

    This worked out for me without using any external libraries in Java 8

    String sampleText = "test"
    int n = 3;
    String output = String.join("", Collections.nCopies(n, sampleText));
    System.out.println(output);
    

    And the output is

    testtesttest
    

提交回复
热议问题