Left padding a string with spaces.

Question:
Is there an easy way to left pad a list of strings making them all the same size?

Answer:
Use the String format() method.

Code:
public class LeftPaddingStrings {
   public static void main(String[] args) {
 
      String strings[] = new String[] {"Tom","Java","Fish","Linux"};
 
      // total size of string
      int n = 8;
 
      System.out.println("Words:");
 
      for (String str1: strings) {
         String str2 = String.format("%0$"+n+"s", str1);
         System.out.println(str2);
      }
   }
}

Output:
$ java LeftPaddingStrings 
Words:
     Tom
    Java
    Fish
   Linux