ProAnswers.org

How to use right and left align strings?

public class Main {

  public static void main(String args[]) {

    String format = "|%1$-10s|%2$10s|%3$-20s|

";

    System.out.format(format, "A", "AA", "AAA");

    System.out.format(format, "B", "", "BBBBB");

    System.out.format(format, "C", "CCCCC", "CCCCCCCC");

//www . ja va  2 s.c  o m

    String ex[] = { "E", "google.com", "E" };

 

    System.out.format(String.format(format, (Object[]) ex));

  }

}

The code above generates the following result.

|A | AA|AAA |

| |  |BBBBB |

| |      CCCCCC| |

| |gooogle.com|E |