LeftPad in Java
We'll cover the following...
Press + to interact
class LeftPad{public static String leftPad(String inputStr,int paddedLength,char ch) {if (paddedLength <= inputStr.length()) {return inputStr;}StringBuilder sb = new StringBuilder();paddedLength = paddedLength - inputStr.length();for (int i = 0; i < paddedLength; ++i) {sb.append(ch);}sb.append(inputStr);return sb.toString();}public static String leftPad(String inputStr,int paddedLength) {return leftPad(inputStr, paddedLength, '.');}public static void main (String[] args)throws java.lang.Exception {System.out.println(leftPad("1", 1));System.out.println(leftPad("2", 2));System.out.println(leftPad("3", 3));System.out.println(leftPad("4", 4));System.out.println(leftPad("5", 5));System.out.println(leftPad("hello", 7));System.out.println(leftPad("foo", 6));System.out.println(leftPad("foo", 3));System.out.println(leftPad("foobar", 3));System.out.println(leftPad("foo", 6, '?'));}}
to save progress
LeftPad in Python
LeftPad in C++
to save progress