Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this short tutorial, we’ll see how to pad a String in Java. We’ll focus mainly on a left pad, meaning that we’ll add the leading spaces or zeros to it until it reaches the desired length.

The approach for the right padded String is very similar, so we’ll only point out the differences.

2. Pad a String Using Custom Methods

The String class in Java doesn’t provide a convenient method for padding, so let’s create several methods on our own.

First, let’s set some expectations:

assertEquals("    123456", padLeftZeros("123456", 10));
assertEquals("0000123456", padLeftZeros("123456", 10));

2.1. Using StringBuilder

We can achieve this with StringBuilder and some procedural logic:

public String padLeftZeros(String inputString, int length) {
    if (inputString.length() >= length) {
        return inputString;
    }
    StringBuilder sb = new StringBuilder();
    while (sb.length() < length - inputString.length()) {
        sb.append('0');
    }
    sb.append(inputString);

    return sb.toString();
}

We can see here that if the original text’s length is equal to or bigger than the desired length, we return the unchanged version of it. Otherwise, we create a new String, starting with spaces, and adding the original one.

Of course, if we wanted to pad with a different character, we could just use it instead of a 0.

Likewise, if we want to right pad, we just need to do new StringBuilder(inputString) instead and then add the spaces at the end.

2.2. Using substring

Another way to do the left padding is to create a String with the desired length that contains only pad characters and then use the substring() method:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
    sb.append(' ');
}

return sb.substring(inputString.length()) + inputString;

2.3. Using String.format

Finally, since Java 5, we can use String.format():

return String.format("%1$" + length + "s", inputString).replace(' ', '0');

We should note that by default the padding operation will be performed using spaces. That’s why we need to use the replace() method if we want to pad with zeros or any other character.

For the right pad, we just have to use a different flag: %1$-.

3. Pad a String Using Libraries

Also, there are external libraries that already offer padding functionalities.

3.1. Apache Commons Lang

Apache Commons Lang provides a package of Java utility classes. One of the most popular ones is StringUtils.

To use it, we’ll need to include it into our project by adding its dependency to our pom.xml file:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.14.0</version>
</dependency>

And then we pass the inputString and the length, just like the methods we created.

We can also pass the padding character:

assertEquals("    123456", StringUtils.leftPad("123456", 10));
assertEquals("0000123456", StringUtils.leftPad("123456", 10, "0"));

Again, the String will be padded with spaces by default, or we need to explicitly set another pad character.

There are also corresponding rightPad() methods.

To explore more features of the Apache Commons Lang 3, check out our introductory tutorial. To see other ways of the String manipulation using the StringUtils class, please refer to this article.

3.2. Google Guava

Another library that we can use is Google’s Guava.

Of course, we first need to add it to the project by adding its dependency:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

And then we use the Strings class:

assertEquals("    123456", Strings.padStart("123456", 10, ' '));
assertEquals("0000123456", Strings.padStart("123456", 10, '0'));

There is no default pad character in this method, so we need to pass it every time.

To right pad, we can use padEnd() method.

The Guava library offers many more features, and we have covered a lot of them. Look here for the Guava-related articles.

4. Conclusion

In this quick article, we illustrated how we can pad a String in Java. We presented examples using our own implementations or existing libraries.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW