fork download
  1. import java.util.Arrays;
  2.  
  3. class Ideone {
  4. public static void main (String[] args) {
  5. System.out.println(padLeft("This", 10, '0'));
  6. System.out.println(padLeft("This is", 10, '0'));
  7. System.out.println(padLeft("This is a", 10, '0'));
  8. System.out.println(padLeft("This is a test", 10, '0'));
  9. }
  10. public static String padLeft(String value, int width, char pad) {
  11. if (value.length() >= width)
  12. return value;
  13. char[] buf = new char[width];
  14. int padLen = width - value.length();
  15. Arrays.fill(buf, 0, padLen, pad);
  16. value.getChars(0, value.length(), buf, padLen);
  17. return new String(buf);
  18. }
  19. }
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
000000This
000This is
0This is a
This is a test