How can I pad a String in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
%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.
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:
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.formatwhen you need a custom pad character such as0. - 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.formatfor 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.
Related reading
- How can I parse/format dates with LocalDateTime? (Java 8)
- How can I pass a parameter to a Java Thread?
- How can I play sound in Java?
- How can I produce messages with Kafka 8.2 API in Java?
- How can I propagate exceptions between threads?
- How can I properly compare two Integers in Java?
- How can I provide different database configurations with Spring Boot?
- How can I read a large text file line by line using Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.