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));
}
}
uscita:Come centrare una stringa usando String.format?
|FirstName |Init. |LastName |
|Real | |Gagnon |
|John |D |Doe |
|John |F. |Kennedy |
voglio l'uscita per essere centrato. Se non utilizzo il flag '-', l'output sarà allineato a destra.
Non ho trovato un flag per centrare il testo nell'API.
This L'articolo contiene alcune informazioni sul formato, ma nulla sul centro giustifica.
L'output è in realtà una tabella, quindi qualche domanda di motivazione non mostra correttamente la tabella. – rana
Vedere '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) ** o ** [http://www.java2s.com/Code/Java/Data-Type/CentersaStringinalargerStringofsizesizeusingthespacecharacter .htm] (http://www.java2s.com/Code/Java/Data-Type/CentersaStringinalargerStringofsizesizeusingthespacecharacter.htm) – ecle
@eee è StringUtils nella libreria standard? La libreria – rana