Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What ways can you create a string with 2000 "spaces"

Tags:

java

oracle

For various reasons I am trying to set a string to 2000 spaces. Currently I am using:

String s = String.format("%1$-2000s"," ");

This is great for Java 5, however, some of the developers in our department are using 1.4 and this does not work.

I was wondering, are any other ways of achieving the same result? I know I can do things like a for loop adding a space at a time, but I am looking for something simple like the format option.

For those that may be interested in why I need this, it is because we have an XML type on a dataobject that on insert into the DB is null. It then gets updated with the XML string, usually around 2000 characters in size. In Oracle pre-reserving this space can prevent row migration, therefore, increasing performance.

Thanks!

like image 651
northpole Avatar asked Jul 13 '10 20:07

northpole


2 Answers

char[] spacesArray = new char[2000];
Arrays.fill(spacesArray, ' ');
String spaces = new String(spacesArray);
like image 108
unbeli Avatar answered Sep 21 '22 17:09

unbeli


the simplest answer: (scroll to see all the codes)

String s = "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "; // 2000 spaces
like image 35
irreputable Avatar answered Sep 24 '22 17:09

irreputable