public class Divers {
public static void main(String args[]){
String format = "|%1$-10s|%2$-10s|%3$-20s|\n";
System.out.format(format, "FirstName", "Init.", "LastName");
System.out.format(format, "Real", "", "Gagnon");
System.out.format(format, "John", "D", "Doe");
String ex[] = { "John", "F.", "Kennedy" };
System.out.format(String.format(format, (Object[])ex));
}
}
output:
|FirstName |Init. |LastName |
|Real | |Gagnon |
|John |D |Doe |
|John |F. |Kennedy |
I want the output to be centered. If I do not use ‘-‘ flag the output will be aligned to the right.
I did not find a flag to center text in the API.
This article has some information about format, but nothing on centre justify.
I quickly hacked this up. You can now use
StringUtils.center(String s, int size)inString.format.