Java – Right pad a string with spaces or zeros

Java programs to add right padding to a string in such a way that total string length should be a fixed predefined number.

For example, if we have a string of length 10, and we want to increase it’s length to 15 – by adding right padding then use the example given this post.

1. How right padding is added

When you add right padding, you essentially add a character repeatedly to the end of string – until string length reaches to defined length.

For example –

howtodoinjava.com		//no padding
howtodoinjava.com    		//right padding of 4 spaces
howtodoinjava.com....		//right padding of 4 dots
howtodoinjava.com0000		//right padding of 4 zeros

Read More: Java remove trailing whitespaces from String

2. Java right padding with spaces

To add right padding, most useful and easy way is to use StringUtils.rightPad() method.

2.1. Maven dependency

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

2.2. Method Syntax (overloaded)

/**
 * @param str  		- the String to pad out, may be null
 * @param size 		- the size to pad to i.e. Total length of result string
 * @param padChar  	- the character or string to pad with
 * 
 * @return right padded String or original String if no padding is necessary or if null String input
 */

public static String rightPad(final String str, final int size) {...}
public static String rightPad(final String str, final int size, String padStr) {...}
public static String rightPad(final String str, final int size, final char padChar) { ... }

2.3. Right padding example

//Added # character to verify padded blank spaces

System.out.println( StringUtils.rightPad("howtodoinjava", 20, " ") + "#");
System.out.println( StringUtils.rightPad("howtodoinjava", 30, " ") + "#");
System.out.println( StringUtils.rightPad("howtodoinjava", 15, " ") + "#");

Program output:

howtodoinjava       #
howtodoinjava                 #
howtodoinjava  #

3. Java right pad a string with zeros

Java program to use StringUtils.rightPad() method to right pad a string with zeros, by adding trailing zeros to string.

System.out.println( StringUtils.rightPad("0123456789", 10, "0") );
System.out.println( StringUtils.rightPad("0123", 10, "0") );
System.out.println( StringUtils.rightPad("012345", 10, "0") );

Program output:

0123456789
0123000000
0123450000

4. Summary

In above examples, we learned to right pad string with spaces to fixed length. We also saw to right pad a number with zeros.

Use this right padding to format strings to fixed length – to display in UI.

Happy Learning !!

Reference:

StringUtils Java Doc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “Java – Right pad a string with spaces or zeros”

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.