supman
V2EX  ›  问与答

java 在输出很长 或者 很短的字符串时 怎么对齐呢?

  •  
  •   supman · Oct 10, 2015 · 3627 views
    This topic created in 3893 days ago, the information mentioned may be changed or developed.

    比如 怎么让 1 2 3 4 也对齐呢?

    fasdkjafsdjkl 1
    safd 2
    dsfdsafadsafsdfdsajlkfadsjlkasdfjkl 3
    afsdadsf 4

    目前尝试了 printf 以及 print \t \t \t 都一点用没有。。

    3 replies    2015-10-10 12:40:49 +08:00
    ljbha007
        1
    ljbha007  
       Oct 10, 2015
    System.out.printf("%-12s%-12s%s\n","Column 1","Column 2","Column3");


    http://examples.javacodegeeks.com/core-java/lang/string/java-string-format-example/
    supman
        2
    supman  
    OP
       Oct 10, 2015
    之前试了一个类似的 % 10s , string 但是没用。。。 我试试你这个:
    supman
        3
    supman  
    OP
       Oct 10, 2015
    @ljbha007 谢谢啦 也终于找到原因了。 我前面那段字符实在是太长了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   900 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 19:27 · PVG 03:27 · LAX 12:27 · JFK 15:27
    ♥ Do have faith in what you're doing.