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.
000000000000154The 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.
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
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:
000000000000ageNow 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
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:
000000000000ageNow 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; } }