0

I have an application that is creating a flat file for a legacy application. Something that is common when creating flat files is having to "pad" empty fields with either zeros or spaces. Today, the way that problem was solved is to have all classes create a string literal for this padded space which is ugly and hard to read and verify the length of a given field.

I want to replace this with a common function like the following:

public String pad(int len, char c) {
    StringBuffer sb = new StringBuffer();
    for (int i=0; i<len; i++) {
        sb.append(c);
    }
    return sb.toString();
}

However I'm concerned about performance as this function would be used fairly prevalently within the codebase. My mind then gravitated towards wrapping this function in a cache (multi-key hashmap, aka hashtable) but I feel like there has to be a more standard way of doing something like this.

What is the least silly way to accomplish this goal?

Russ
  • 1,866
  • 3
  • 18
  • 28
  • If you know how long the string will be, you should pass that length to the `StringBuilder`: `StringBuffer sb = new StringBuffer(len);` Also, you should be using `StringBuilder` here, not `StringBuffer` since you don't need the latter's thread-safe features. – bradimus May 23 '17 at 13:31
  • See [How can I pad a String in Java](https://stackoverflow.com/q/388461/205233) – Filburt May 23 '17 at 13:33
  • Fastest is not necessarily the least silly. If maintainability is more important, then there are many third party libraries have string padding functions. However, if performance is really your main priority then you should be memory mapping the file and using a direct `ByteBuffer` to write the padding characters. – SimonC May 23 '17 at 13:38

3 Answers3

1

If what matters is performance I would suggest

public static String pad(int len, char c) {
    char[] charArray = new char[len];
    Arrays.fill(charArray, c);
    return new String(charArray);
}
Francesco Pitzalis
  • 1,752
  • 13
  • 20
1

If you don't want to use a third party library (which you should, since they're more obvious as to what is going on), you could do:

public String pad(int len, char c) {
    final char[] array = new char[len];
    Arrays.fill(array, c);
    return new String(array);
}
SimonC
  • 6,382
  • 21
  • 40
  • Code is good, but I strongly disagree with your statement about a third party library. Dependencies make programs less portable, and most third party libraries are poorly managed. – VGR May 23 '17 at 13:42
  • I take you point, but in this case I think you would struggle to accuse the Apache commons library as poorly managed. I've tried (and failed) to reinvent the wheel many times because I thought I could do it *slightly* better. – SimonC May 23 '17 at 13:46
0

If you are using java 8, then use the join function:

String.join("", Collections.nCopies(numberOfTimes, repeatString));

Otherwise use,

return  new String(new char[numberOfTimes]).replace("\0", repeatString);
sreehari
  • 159
  • 3
  • 15