Hmm now that I think about it, maybe Arrays.fill
:
char[] charArray = new char[length];
Arrays.fill(charArray, ' ');
String str = new String(charArray);
Of course, I assume that the fill
method does the same thing as your code, so it will probably perform about the same, but at least this is fewer lines.