Right Pad a String with Spaces or Zeros

This tutorial demonstrates how to add the right padding to a string so that the total length of the padded string should be a fixed number. For example, if we have a string of length 10, and we want to increase its length to 15 – by adding the right padding.

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

1. Using Plain Java

A good way to start is to create a custom API that caters to all the requirements. The following method use String.repeat() API to create the right pad and then append it to the input string to build the final string.

public static String rightPad(String input, int length, String padStr) {

  if(input == null || padStr == null){
    return null;
  }

  if(input.length() >= length){
    return input;
  }

  int padLength = length - input.length();

  StringBuilder paddedString = new StringBuilder();
  paddedString.append(input);
  paddedString.append(padStr.repeat(padLength));

  return paddedString.toString();
}

We can test the rightPad() method as follows:

Assertions.assertEquals("1234567890", rightPad("123456789", 10, "0"));
Assertions.assertEquals("howtodoinjava       ", rightPad("howtodoinjava", 20, " "));

2. Using StringUtils.rightPad() API

The StringUtils class provides useful methods to modify the strings to get desired effects. Its rightPad() adds the right-padding using the specified pas character or string. Use StringUtils.leftPad() to left pad a String in Java.

public static String rightPad(str, size);
public static String rightPad(str, size, padStr);
public static String rightPad(str, size, padChar);

2.1. Maven

Start with adding the latest version of Apache common-lang3 library in the project.

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

2.2. Right Padding with Spaces

In the following program, we are right padding the string “java”. The empty space is used as the pad character.

Assertions.assertEquals(null, StringUtils.rightPad(null, 50, " "));
Assertions.assertEquals("java ", StringUtils.rightPad("java", 5, " "));
Assertions.assertEquals("java      ", StringUtils.rightPad("java", 10, " "));

2.2. Right Padding with Zeros

In the following program, we are right padding the number “123”. The pad character used is zero.

Assertions.assertEquals("12300", StringUtils.rightPad("123", 5, "0"));
Assertions.assertEquals("1230000000", StringUtils.rightPad("123", 10, "0"));

3. Using Strings.padEnd()

The Strings class is part of the Guava library and provides utility methods for modifying the strings. Start with adding the latest version of Guava to the project.

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

Now we can use the Strings.padEnd() API for adding the right padding as follows:

Assertions.assertEquals("1234567890", Strings.padEnd("123456789", 10, '0'));
Assertions.assertEquals("howtodoinjava       ", Strings.padEnd("howtodoinjava", 20, ' '));

4. Conclusion

In this Java String tutorial, we learned to right-pad a string with spaces, zeros or any pad character to modify the string to make it of a fixed length. We learned to use the StringUtils, Strings classes and create our own solution for the right padding.

Happy Learning !!

Sourcecode on Github

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial