Grails Cookbook - A collection of tutorials and examples

Left Pad String With Zeros in Java

What if you want to pad String With Zeroes in it? This is also possible. A lot of programmers see the efficiency of the Left Pad String with either zeroes or spaces in it. Study and learn the examples written below on how to do this process.

Sometimes we wish a numeric value like this:

154

To look like below. The value is the same but we converted it to a String with padded zeroes to the left. Sometimes this is needed in some programs, perhaps to be consumed by another system and it needs to follow the format.

000000000000154

The output shows 12 zeroes were added to the left of the String to achieve a 15-String length. And how to do this is what we will discuss below.

Convert Using Substring Trick

Below is a simple example that utilizes substring to pad a String. Check it out:
public class TricktLeftPadSample {
   public static void main(String[] args) {
      String theOriginalString = "age";
      String leftPaddedString = "000000000000".substring(theOriginalString.length()) + theOriginalString;
      System.out.println(leftPaddedString);
   }
}
Another way to perform a substring is to create first a series of zeroes. Observe the altered program below for a deeper understanding.
public class LeftPadZeroesSample {
   public static void main(String[] args) {
      System.out.println(leftPadZeroes("age", 15));
   }
   public static String leftPadZeroes(String theOriginalString, int length) {
      StringBuilder sb = new StringBuilder();
      for (int i=0; i < length; i++) {
         sb.append("0");
      }
      String padding = sb.toString();
      String leftPaddedString = padding.substring(theOriginalString.length()) + theOriginalString;
      return leftPaddedString;
   }
}

The same output is shown below:

000000000000age

Convert Using Simple Concatenation

First, we can try one simple way. This is a program that performs left padding in order to use a simple String concatenation.
public class SimpleConcatTest {
   public static void main(String[] args) {
      String theOriginalString = "age";
      String leftPaddedString = theOriginalString;
      while (leftPaddedString.length() < 15) {
         leftPaddedString = "0" + leftPaddedString;
      }
      System.out.println(leftPaddedString);
   }
}

Until the required length of the String is met, the String or character "0" is padded to the left of the String "age". This is what the condition of while loop is used for.

Check out the expected result below:

000000000000age
Now let's try a different approach with a separate function.
public class AnotherConcatTest {
    public static void main(String[] args) {
      System.out.println(leftPadZeroes("age", 15));
   }
   public static String leftPadZeroes(String theOriginalString, int length) {
      String leftPaddedString = theOriginalString;
      while (leftPaddedString.length() < length) {
         leftPaddedString = "0" + leftPaddedString;
      }
      return leftPaddedString;
   }
}

The same implementation applies:

000000000000age

Convert Using StringBuilder

Using StringBuilder to Left Pad zeroes before the String is efficient also. Know that it's also possible to use StringBuffer. Below is the example of the process using StringBuilder.
public class LeftPadSBTest {
   public static void main(String[] args) {
      String theOriginalString = "age";
      StringBuilder sb = new StringBuilder();
      while (sb.length() + theOriginalString.length() < 15) {
         sb.append('0');
      }
      sb.append(theOriginalString);
      String leftPaddedString = sb.toString();
      System.out.println(leftPaddedString);
   }
}

The same output is shown below:

000000000000age
Now let's do the same implemention with a modified version that achieves a target length.
public class AnotherSampleTest {
   public static void main(String[] args) {
      System.out.println(leftPadZeroes("age", 15));
   }
   public static String leftPadZeroes(String theOriginalString, int length) {
      StringBuilder sb = new StringBuilder();
      while (sb.length() + theOriginalString.length() < 15) {
         sb.append('0');
      }
      sb.append(theOriginalString);
      String leftPaddedString = sb.toString();
      return leftPaddedString;
   }
}