Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
leftpad
stringutils

What is the StringUtils.leftPad method in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

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

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

How to 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 leftPad(final String str, final int size) method

This 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)

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 left padded string.

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

This 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)

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 left padded string.

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

This 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)

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 left padded string.

Example

<?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>

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 i.e numPadding.
  • Lines 8–10: We pass 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.
  • Line 12: We define the padding character i.e paddingChar.
  • Lines 13–15: We invoke the 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.

RELATED TAGS

java
communitycreator
leftpad
stringutils

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring