In Java, rightPad()
is a StringUtils
, which is used to right pad spaces to the given string. There are three variations of the method:
rightPad(final String str, final int size)
rightPad(final String str, final int size, final char padChar)
rightPad(final String str, final int size, String padStr)
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;
rightPad(final String str, final int size)
methodThis 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)
final String str
: The string to be padded.final int size
: The final length of the string after padding.This method returns a new right padded string.
rightPad(final String str, final int size, final char padChar)
methodThis 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)
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.This method returns a right padded string.
rightPad(final String str, final int size, String padStr)
methodThis 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)
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.This method returns a new right padded string.
<?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>
The maven dependency for StringUtils
is included in the pom.xml
file:
Main.java
StringUtils
class.String
called text
.numPadding
.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
.paddingChar
.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
.