In Java leftPad()
is a StringUtils
class which is used to left pad spaces to the given string. There are three variations of the method:
leftPad(final String str, final int size)
leftPad(final String str, final int size, final char padChar)
leftPad(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;
leftPad(final String str, final int size)
methodThis method is used to pad spaces to the left 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, then the given string is returned as-is, without padding.
public static String leftPad(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 left padded string.
leftPad(final String str, final int size, final char padChar)
methodThis method is used to pad the given character to the left 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, the given string is returned as is without padding.
public static String leftPad(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 left padded string.
leftPad(final String str, final int size, String padStr)
methodThis method is used to pad the given string (padStr
) to the left 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, then the given string is returned as is without padding.
leftPad(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 left 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
.text
and numPadding
as parameters to invoke the leftPad()
method. The output is stored in paddedText
. The output is spaces padded to the left of text
. Then paddedText
is printed.paddingChar
.leftPad()
method passing text
, numPadding
and paddingChar
as parameters. The output is stored in paddedText
. The output is the paddingChar
padded to the left of text
. paddedText
is printed.