Java
Coding
String Manipulation
Programming
Java Padding

How can I pad a String in Java?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Padding a string means extending it to a target length by adding characters on the left or right. It is commonly used for console output, fixed-width files, report generation, and ID formatting.

In Java, the best approach depends on what kind of padding you need. Spaces are easy with String.format, while custom characters such as 0 or * often need either a small helper method or a utility library.

Padding with Spaces Using String.format

For simple output formatting, String.format is the built-in option most developers reach for first.

java
1public class Main {
2    public static void main(String[] args) {
3        String text = "cat";
4
5        String leftPadded = String.format("%5s", text);
6        String rightPadded = String.format("%-5s", text);
7
8        System.out.println("'" + leftPadded + "'");
9        System.out.println("'" + rightPadded + "'");
10    }
11}
text
'  cat'
'cat  '

%5s right-aligns the string in a field of width five, which means padding appears on the left. %-5s left-aligns it, so padding appears on the right.

This is simple and readable, but it only pads with spaces.

Padding with a Custom Character

If you need zeros or some other character, a helper method gives you more control.

java
1public class Main {
2    static String leftPad(String value, int length, char padChar) {
3        if (value.length() >= length) {
4            return value;
5        }
6        return String.valueOf(padChar).repeat(length - value.length()) + value;
7    }
8
9    static String rightPad(String value, int length, char padChar) {
10        if (value.length() >= length) {
11            return value;
12        }
13        return value + String.valueOf(padChar).repeat(length - value.length());
14    }
15
16    public static void main(String[] args) {
17        System.out.println(leftPad("42", 5, '0'));
18        System.out.println(rightPad("ID", 6, '.'));
19    }
20}
text
00042
ID....

This approach is easy to test and avoids adding an external dependency just for padding.

Using a Utility Library

If your project already uses Apache Commons Lang, StringUtils.leftPad and StringUtils.rightPad are convenient:

java
1import org.apache.commons.lang3.StringUtils;
2
3public class Main {
4    public static void main(String[] args) {
5        System.out.println(StringUtils.leftPad("42", 5, '0'));
6        System.out.println(StringUtils.rightPad("ID", 6, '.'));
7    }
8}

These methods are especially handy when string manipulation appears frequently across a codebase. If the library is not already present, however, a tiny helper can be more sensible than adding a dependency for one feature.

Things to Consider Before Padding

Padding is about display or serialization, not about changing the original meaning of the data. If you are padding numbers for output, keep the underlying numeric value separate from the formatted string.

It is also worth deciding what should happen when the input is already longer than the requested width. Most padding helpers simply return the original string unchanged, which is usually safer than truncating data silently.

Finally, remember that String.length() counts UTF-16 code units, not visible character width on screen. For simple ASCII output that is fine, but console alignment with complex Unicode text can be harder than it first appears.

Common Pitfalls

  • Using String.format when you need a custom pad character such as 0.
  • Forgetting to handle inputs that are already longer than the target length.
  • Mixing formatting concerns with data storage, such as storing padded IDs instead of formatting them at output time.
  • Assuming visible display width always matches String.length().

Summary

  • Use String.format for simple space padding.
  • Use a helper method or a library utility for custom padding characters.
  • Return the original string when it already meets or exceeds the target width unless truncation is explicitly required.
  • Keep padded strings as presentation values, not raw data.
  • Be careful with alignment if your text includes complex Unicode characters.

Course illustration
Course illustration

All Rights Reserved.