Java printff string only output formatting

烈酒焚心 提交于 2019-12-07 13:27:44

I think you're looking for the Formatter class, which does printf-style formatting for Java.

For example, applied to your input:

    StringBuilder sb = new StringBuilder();
    Formatter formatter = new Formatter(sb, Locale.US);

    formatter.format("%1$-15s %2$-15s %3$-15s %4$-15s\n", "testing", "testing", "testing", "testing");
    formatter.format("%1$-15s %2$-15s %3$-15s %4$-15s", "testingggg", "testingggg", "testingggg", "testingggg");

    System.out.println(sb.toString());

Produces:

testing         testing         testing         testing        
testingggg      testingggg      testingggg      testingggg    
Sophie Pocket

You can use System.out.printf("%sggg" , t)

Where t = "testing" and ggg is just appended onto this string.

Hope this helps :D

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!