Introduction
In this article, I will introduce several methods for padding strings with zeros or spaces in Java, along with a comparison of their execution speeds.
At the end of the article, I will summarize which method is the most efficient to use.
Using String.format
You can pad strings with zeros or spaces using the format()
method of the String
class.
This is probably the most conventional method and is likely the top result when searching for “Java zero padding.”
It can be implemented using the standard library and requires only one line of code, making it easy to use. However, it has the drawback of slower execution speed.
Below is an example of adding zeros to a number that has fewer than 8 digits.
int target = 1234;
String result = String.format("%08d", target);
System.out.println(result);
// 00001234
- In
"%08d"
, the “0” represents the padding character, and the “8” represents the total number of digits. If you want to pad with spaces instead of zeros, use"% 8d"
as the argument. For more details, refer to the official documentation.
The issue lies in its execution speed. In my environment, running String.format()
one million times took 1,450 milliseconds. Other methods for zero-padding (or space-padding) are discussed below, but String.format()
is the slowest of them all.
If you require faster processing, it’s better to avoid using String.format()
.
Using Third-Party Libraries
Apache Commons Lang is a third-party library that provides a range of generic functionalities and is likely used in many projects. This library also offers methods for padding characters into strings.
The following example, similar to the one above, demonstrates adding zeros to a number that has fewer than 8 digits.
import org.apache.commons.lang3.StringUtils;
// ...Omitted...
String target = "1234";
String result = StringUtils.leftPad(target, 8, '0');
// 00001234
- You can use
StringUtils.leftPad()
to pad with zeros or spaces.
The execution speed is significantly faster as well. Running StringUtils.leftPad()
one million times took only 59 milliseconds. This is more than 10 times faster than the String.format()
method introduced earlier.
If your project already uses Apache Commons Lang, I recommend using StringUtils.leftPad()
instead of String.format()
.
Like Apache Commons Lang, Google Guava also provides methods for padding characters. Google Guava is also widely used in many projects, and some projects may prefer to use Google Guava instead of Apache Commons Lang.
The following is an example of adding zeros to a number with fewer than 8 digits.
import com.google.common.base.Strings;
// ...Omitted...
String target = "1234";
String result = Strings.padStart(target, 8, '0');
// 00001234
- You can use
Strings.padStart()
to pad with zeros or spaces.
The execution speed is sufficiently fast. Based on my testing, the speed was almost identical to that of using StringUtils.leftPad()
from Apache Commons Lang.
As before, if your project uses Google Guava, I recommend using Google Guava’s method instead of String.format().
Creating Your Own Method
If you cannot use third-party libraries such as Apache Commons Lang or Google Guava, are not satisfied with the speed of String.format()
, or want to implement your own padding logic, you can create a custom method.
I have created a generic padding method.
public final class StringUtil {
private StringUtil() {}
public static String padLeft(String target, int length, char padChar) {
int targetLength = target.length();
if (targetLength >= length) {
return target;
}
StringBuilder sb = new StringBuilder(length);
for (int i = targetLength; i < length; i++) {
sb.append(padChar);
}
sb.append(target);
return sb.toString();
}
}
- The arguments are similar to those in
StringUtils.leftPad()
from Apache Commons Lang andStrings.padStart()
from Google Guava. The first argument is the string to be padded, the second argument is the total number of digits, and the third argument is the padding character.
When using this method for zero-padding, the process would look like this:
String target = "1234";
String result = StringUtil.padLeft(target, 8, '0');
// 00001234
Inside this custom padding method, StringBuilder
is used to concatenate the characters, so processing speed is not an issue. Based on my tests, the speed was nearly identical to that of StringUtils.leftPad()
from Apache Commons Lang.
Conclusion
There are several ways to perform zero-padding and space-padding, but it’s best to avoid using String.format()
due to its slower execution speed.
If you have access to third-party libraries like Apache Commons Lang or Google Guava, it’s recommended to use them.
If that’s not possible, consider creating your own padding method and using that instead.
Comments