Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Left Padding with space at the start of String in Java

  1. //Left Padding With Space at the start of the String
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5.  
  6. class LeftPaddingwithSpace
  7. {
  8. public static void main(String[] argv) {
  9. String s=padLeft("aaaa", 10);
  10. System.out.println(">" + s + "<" );
  11. }
  12.  
  13. public static String padLeft(String s, int n) {
  14. return String.format("%0$"+n+"s", s);
  15. }
  16. }


Output:
>aaaa<