Left Pad a String with Spaces or Zeros in Java

Learn to left pad to a string in Java to make it to a fixed length. For example, if we have a string of length 10, and we want to increase its length to 15 – by adding spaces to its left. Similarly, we can left pad a number by adding zeros to its left.

The following example demonstrates the effect of left-padding adding to the string or numbers.

howtodoinjava.com       //no padding
    howtodoinjava.com   //left padding of 4 spaces
....howtodoinjava.com   //left padding of 4 dots
0000howtodoinjava.com   //left padding of 4 zeros

1. Using StringUtils.leftPad()

To add left padding, the most simple and easy way is to use StringUtils.leftPad() method. The StringUtils class provides many useful methods to manipulate the strings and modify them to get desired effects.

Use StringUtils.rightPad() to right pad a String in Java.

1.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>

1.2. Syntax

The leftPad() method is an overloaded method with the following parameters:

  • str – the String to pad. It can be null. It does not throw NullPointerException, rather it returns null when null argument is passed.
  • size – the size to pad i.e. Total length of the result string.
  • padStr – the string to pad with.
  • padChar – the character to pad with.
public static String leftPad(str, size);
public static String leftPad(str, size, padStr);
public static String leftPad(str, size, padChar);

1.3. Left Padding with Spaces

In the following program, we are left padding the string “howtodoinjava”. The second statement does not add any padding because the length of the input string is more than 10, already.

Assertions.assertEquals(null, StringUtils.leftPad(null, 10, " "));
Assertions.assertEquals("howtodoinjava", StringUtils.leftPad("howtodoinjava", 10, " "));
Assertions.assertEquals("       howtodoinjava", StringUtils.leftPad("howtodoinjava", 20, " "));

1.4. Left Padding with Zeros

The following Java program uses StringUtils.leftPad() method to left pad a number with zeros, by adding leading zeros to the string.

Assertions.assertEquals("0123456789", StringUtils.leftPad("123456789", 10, "0"));
Assertions.assertEquals("0000000789", StringUtils.leftPad("789", 10, "0"));
Assertions.assertEquals("0000056789", StringUtils.leftPad("56789", 10, "0")); 

2. Using Strings.padStart()

The Strings class is part of Google 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.padStart() API for adding the left padding.

Assertions.assertEquals("0123456789", Strings.padStart("123456789", 10, '0'));
Assertions.assertEquals("       howtodoinjava", Strings.padStart("howtodoinjava", 20, ' '));

3. Using Plain Java

We can create our own custom API to pad a given string with the provided pad character or string. It gives us the flexibility to customize the logic even further. For example, we can decide what to do if any parameter is null.

The following method use String.repeat() API to create the left pad and then append the input string to build the final string.

public static String leftPad(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(padStr.repeat(padLength));
  paddedString.append(input);

  return paddedString.toString();
}

Let us test out the function:

Assertions.assertEquals("0123456789", leftPad("123456789", 10, "0"));
Assertions.assertEquals("       howtodoinjava", leftPad("howtodoinjava", 20, " "));

4. Conclusion

In this short Java tutorial, we learned to left-pad a string with spaces (or any pad character) to a fixed length. We also saw to the left pad a number with zeros. We learned to use the StringUtils, Strings classes and create our own solution for left-padding.

Happy Learning !!

Reference: StringUtils Java Doc

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