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; } }
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:
HELLOTake 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; } }
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; } }