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));
}
}
wyjściowa:Jak wyśrodkować ciąg za pomocą String.format?
|FirstName |Init. |LastName |
|Real | |Gagnon |
|John |D |Doe |
|John |F. |Kennedy |
Chcę wyjście do centrum. Jeśli nie użyję flagi "-", wynik zostanie wyrównany w prawo.
Nie znalazłem flagi, aby wyśrodkować tekst w interfejsie API.
This artykuł zawiera informacje o formacie, ale nic w centrum nie uzasadnia.
Dane wyjściowe są w rzeczywistości tabelami, więc niektóre pytania powodują nieprawidłowe wyświetlanie tabeli. – rana
Zobacz 'StringUtils' [http://stackoverflow.com/questions/2709993/how-to-center-string-output-using-printf-and-variable-width-java](http://stackoverflow.com/questions/2709993/how-to-center-string-output-using-printf-and-variable-width-java) ** lub ** [http://www.java2s.com/Code/Java/Data-Type/CentersaStringinalargerStringofsizesizeusing thespacecharacter .htm] (http://www.java2s.com/Code/Java/Data-Type/CentersaStringinalargerStringofsizesizeusing the spacechar.htm) – ecle
@eee to StringUtils w standardowej bibliotece? – rana