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:
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
counttimes.If this string is empty or
countis zero then the empty string is returned.
So instead of a loop your code would just look like this:
" ".repeat(length);