Print-only output string printff

How to format output using printf.

Suppose I want to print the following in formatting.

testing testing testing testing testingggg testingggg testingggg testingggg 

I do not ask about the use of "\ t", I ask about formatting in printf format? If someone can give me a suggestion and an example. Or there may be a link to an example. I can do this to fit my needs.

+1
source share
2 answers

I think you're looking for a Formatter class that does printf formatting for Java.

For example, applies 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()); 

It produces:

 testing testing testing testing testingggg testingggg testingggg testingggg 
+8
source

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

Where t = "testing" and ggg simply added to this line.

Hope this helps: D

0
source

All Articles