Introducing Radical.sh

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

Right padding zeros to end of a string in Java

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


Output:
>aaaa000000<