Create a string with n characters.

Posted by C. Ross on Stack Overflow See other posts from Stack Overflow or by C. Ross
Published on 2010-05-10T17:21:46Z Indexed on 2010/05/10 17:24 UTC
Read the original article Hit count: 169

Filed under:
|

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:

StringBuffer outputBuffer = new StringBuffer(length);
for (int i = 0; i < length; i++){
   outputBuffer.append(" ");
}
return outputBuffer.toString();

Is there a better way to accomplish the same thing. In particular I'd like something that is fast (in terms of execution).

© Stack Overflow or respective owner

Related posts about java

Related posts about string