What is the StringUtils.rightPad method in Java?

In Java, rightPad() is a staticThe methods in Java that can be called without creating an object of the class. method of the StringUtils, which is used to right pad spaces to the given string. There are three variations of the method:

  1. rightPad(final String str, final int size)
  2. rightPad(final String str, final int size, final char padChar)
  3. rightPad(final String str, final int size, String padStr)

Import StringUtils

The definition of StringUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


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

Note: For other versions of the commons-lang package, refer to the Maven Repository.

We can import the StringUtils class as follows:


import org.apache.commons.lang3.StringUtils;

The rightPad(final String str, final int size) method

This method is used to pad spaces to the right side of the string. The number of spaces padded is equal to the difference of the specified size and the length of the given string.

If the size parameter is negative or less than the length of the given string, it returns the given string without padding.


public static String rightPad(final String str, final int size)

Parameters

  • final String str: The string to be padded.
  • final int size: The final length of the string after padding.

Return value

This method returns a new right padded string.

The rightPad(final String str, final int size, final char padChar) method

This method is used to pad the given character to the right side of the string. The number of characters padded is equal to the difference of the specified size and the length of the given string.

If the size parameter is negative or less than the length of the given string, it returns the given string without padding.


public static String rightPad(final String str, final int size, final char padChar)

Parameters

  • final String str: The string to be padded.
  • final int size: The final length of the string after padding.
  • final char padChar: The character to pad.

Return value

This method returns a right padded string.

The rightPad(final String str, final int size, String padStr) method

This method is used to pad the given string, padStr, to the right side of the string. The number of pad strings, padStr, padded is equal to the difference of the specified size and the length of the given string.

If the size parameter is negative or less than the length of the given string, it returns the given string without any padding.


rightPad(final String str, final int size, String padStr)

Parameters

  • final String str: The string to be padded.
  • final int size: The final length of the string after padding.
  • String padStr: The string to pad.

Return value

This method returns a new right padded string.

Examples

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
Using StringUtils.rightPad() method

Explanation

The maven dependency for StringUtils is included in the pom.xml file:

Main.java

  • Line 1: We import the StringUtils class.
  • Line 6: We define a String called text.
  • Line 7: We define the size for the padding, numPadding.
  • Lines 8–10: - We invoke the rightPad() method passing text and numPadding as parameters. We store the output in paddedText. The output is the spaces padded to the right of text. We print paddedText.
  • Line 12: We define the padding character, paddingChar.
  • Lines 13–15: We invoke the rightPad() method passing text, numPadding and paddingChar as parameters. We store the output in paddedText. The output is the paddingChar padded to the right of text. We print paddedText.

Free Resources