6

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!

northpole
  • 10,244
  • 7
  • 35
  • 58
  • Insert a single space into a char(2000) field in a database, then select it back out. – aehiilrs Jul 13 '10 at 20:44
  • does this really reserve the entire 2000 block? – northpole Jul 13 '10 at 20:52
  • Which Oracle version and are you using XMLTYPE, if so what is the storage clause for the XMLTYPE? – oluies Jul 13 '10 at 21:03
  • Oracle 10g and I am currently using a CLOB. – northpole Jul 13 '10 at 21:06
  • 4
    This is an unconventional solution to a conventional problem. Rather than filling the field with a dummy value, the preferred way to handle this is to set PCTFREE for the table to a level that allows for the expected growth of each row. – Allan Jul 13 '10 at 21:06
  • We had to move away from XMLTYPE to support the old form of replication that we were doing. We will use XMLTYPE if we move to a product that supports it. We do use PCTFREE but we never anticipated the rows to be this large. So going forward we are good, but not for the 50 million rows that are currently causing issues. – northpole Jul 13 '10 at 21:09
  • re: why you need this - Please please please be sure to document this in the project documentation, the code, and the DB documentation. Your reasoning is sound, but without a good explanation such shenanigans are a bad code smell. – Freiheit Jul 13 '10 at 21:13
  • @Freiheit - I completely agree and have taken the steps to document this in detail. – northpole Jul 13 '10 at 21:14

8 Answers8

18
char[] spacesArray = new char[2000];
Arrays.fill(spacesArray, ' ');
String spaces = new String(spacesArray);
unbeli
  • 29,501
  • 5
  • 55
  • 57
3

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

String s = "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "; // 2000 spaces
irreputable
  • 44,725
  • 9
  • 65
  • 93
  • 1
    +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 Jul 14 '10 at 02:05
2

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
  • 17,141
  • 4
  • 52
  • 69
1

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
  • 73,784
  • 33
  • 194
  • 347
1

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
  • 105,238
  • 25
  • 256
  • 415
0

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
  • 588,226
  • 146
  • 1,060
  • 1,140
0

If nothing else works:

StringBuilder sb = new StringBuilder();
for(int i = 0; i < 2000; ++i)
   sb.append(" ");
String str = new String(sb);
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
0

See this other question.

Can I multiply strings in Java to repeat sequences?

Both Apache Commons StringUtils and Google Guava libraries have commands to multiply (repeat) strings.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154