Create a string with n characters

前端 未结 27 1131
猫巷女王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 23:00

    Since Java 11 you can simply use String.repeat(count) to solve your problem.

    Returns a string whose value is the concatenation of this string repeated count times.

    If this string is empty or count is zero then the empty string is returned.

    So instead of a loop your code would just look like this:

    " ".repeat(length);
    

提交回复
热议问题