Grails Cookbook - A collection of tutorials and examples

Left Pad String With Spaces in Java

When you work on a program or create a system that requires you to pad a String using spaces, all you need to do is to consider the width of a String. If the width is less than the target length, you can pad the String with spaces to guarantee the length of that String. Java left pad string with spaces is important specially when interfacing with older systems like a mainframe, where deciding which part of data is defined by number of characters, instead of more sophisticated way like XML or JSON format.
Observe the written example of Java Left Pad String With Spaces to guarantee the length of a String, understand the codes below and we will try to break down the syntax for a better understanding. At the end of this post, our goal is to have a knowledge on how the process works.

Using Substring

Now let's try performing a substring to determine the String to pad starting with 10 spaces.

Observe the written example below:

public class SampleTrickTest {
   public static void main(String[] args) {
      String theOriginalString = "HELLO";
      String thePaddedString = "          ".substring(theOriginalString.length()) + theOriginalString;
      System.out.println(thePaddedString);
   }
}
Here is a modified version of the code that refactors the logic into a separate function. We first build a series of spaces before we perform a substring on it.

Another option is to build spaces before performing a substring. Observe this modified program below, it refactors the logic into a separate function.

public class modifiedSampleTest  {
   public static void main(String[] args) {
      System.out.println(leftPaddingWithSpaces("HELLO", 10));
   }
   public static String leftPaddingWithSpaces(String theOriginalString, int length) {
      StringBuilder sb = new StringBuilder();

for (int i=0; i < length; i++) { sb.append(" "); } } String padding = sb.toString(); String thePaddedString = padding.substring(theOriginalString.length()) + theOriginalString; return thePaddedString; } }

Using Concatenation

Here's one solution, we can use a simple loop and concatenate a space before the String in order to get the desired length.

Check out the example below:

public class SimpleConcatTest {
   public static void main(String[] args) {
      String theOriginalString = "HELLO";
      String thePaddedString = theOriginalString;
      while (thePaddedString.length() < 10) {
         thePaddedString = " " + thePaddedString;
      }
      System.out.println(thePaddedString);
   }
}
Since the thePaddedString's length is five, the while loop will continue to add a space to the left of the String until it satisfies the condition of the while loop.

Check out the result below:

     HELLO
Take a look at this program, this is the same implementation from the example written above only this time, it refactors the logic into a separate function.
public class AnotherSampleTest {
    public static void main(String[] args) {
      System.out.println(leftPaddingWithSpaces("HELLO", 10));
   }
   public static String leftPaddingWithSpaces(String theOriginalString, int length) {
      String thePaddedString = theOriginalString;
      while (thePaddedString.length() < length) {
         thePaddedString = " " + thePaddedString;
      }
      return thePaddedString;
   }
}

Using StringBuilder

We can also use another method to pad spaces to the left of the String, we can use StringBuilder. Check out the example below:
public class SbSampleTest {
   public static void main(String[] args) {
      String theOriginalString = "HELLO";
      StringBuilder sb = new StringBuilder();
      while (sb.length() + thePaddedString.length() < 10) {
         sb.append(' ');
      }
      sb.append(thePaddedString);
      String thePaddedString = sb.toString();
      System.out.println(thePaddedString);
   }
}
Let's try combining StringBuilder with the function we used a while ago. The goal is still the same: padding spaces to the left of the String in order to satisfy the desired length.
public class TestSample {
   public static void main(String[] args) {
      System.out.println(leftPaddingWithSpaces("HELLO", 10));
   }
   public static String leftPaddingWithSpaces(String theOriginalString, int length) {
      StringBuilder sb = new StringBuilder();
      while (sb.length() + theOriginalString.length() < 10) {
         sb.append(' ');
      }
      sb.append(theOriginalString);
      String thePaddedString = sb.toString();
      return thePaddedString;
   }
}