views:

221

answers:

7

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!

+1  A: 

A StringBuffer and then add a space 2000 times in a loop, and toString() afterwards. I don't think there are any "simpler" ways to do it which doesn't end up doing this anyway under the covers.

If you do this a lot, it would make a good library function.

Thorbjørn Ravn Andersen
You can use a StringBuilder instead in order to avoid the small synchronization overhead, and you can also initialize the builder with the target size so that no realocation takes place.
Eyal Schneider
StringBuilder is not available in Java 1.4 which the original poster asks about.
Thorbjørn Ravn Andersen
+18  A: 
    char[] spacesArray = new char[2000];
    Arrays.fill(spacesArray, ' ');
    String spaces = new String(spacesArray);
unbeli
A: 

StringUtils.repeat(" ", 2000) (from commons-lang)

However, I'm not sure whether such micro-optimizations should be made with the cost of code that would require a 5 line comment to explain why is this needed. If you do it - be sure to add an extensive comment, otherwise imagine the reaction of those reading your code.

Bozho
ya, my function is very well documented for that reason
northpole
@downvoter - could you explain the downvote? You don't like commons-lang, or my note about the comment?
Bozho
+1  A: 

A random function I found in my personal library:

public static String whiteSpace2(int l) {
    if (l==0) return "";
    String half=whiteSpace2(l/2);
    if ((l&1)!=0) {
        return half+" "+half;
    } else {
        return half+half;
    }
}

Not claiming it is the fastest possible way to generate whitespace, but it works :-)

mikera
p.s. I believe this is O(n) time so shouldn't actually be that bad....
mikera
+1 if its a joke, its quite funny.
Viktor Sehr
You really shouldn't be building strings with `+` like this. Use a `StringBuilder`. The `O(log N)` step is nice, though.
polygenelubricants
Just fixed my recurrence formula: F(N)=F(N/2)+N. Yes, it is linear, my mistake.
Eyal Schneider
You do realize that there is a special place in hell for developers who use 'l' as a variable name.
Skip Head
I believe the amount of toStrings() drown the log n versus n discussion.
Thorbjørn Ravn Andersen
All - yes this is a "Joke" in that it was a mildly comical way of writing a completely non-performance-critical utility function :-)
mikera
A: 

If nothing else works:

StringBuilder sb = new StringBuilder();
for(int i = 0; i < 2000; ++i)
   sb.append(" ");
String str = new String(sb);
Viktor Sehr
why not just `sb.toString()`
fuzzy lollipop
+1  A: 

You can use lpad(' ',2000,' ') in the insert statement directly to tell Oracle to create the value you want.

In fact, you can set the field in question to have this as the default, which could prevent you from needing to change it in multiple places (if your code is explicitly sending null as the value for the field, that will override the default).

Allan
+2  A: 

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

String s = "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "; // 2000 spaces
irreputable
Did you count them?
Pascal Thivent
+1 I like this solution. If we know at compile time what the string is, why not hard code it. (However, probably another solution involving the database itself is better.)I tweaked the solution to make it just a little better and to answer Pascal's question. Run it with assertions enabled.String s = ... 2000 spaces ... ;assert s.length()==2000 : "constant string is wrong length." ;
emory