Thursday, 13 April 2017

Java basics - Sting format

String Formatting

Most common way of formatting a string in java is using String.format(). If there were a “java sprintf” then this would be it.
String output = String.format("%s = %d", "joe", 35);

For formatted console output, you can use printf() or the format() method of System.out and System.err PrintStreams.
System.out.printf("My name is: %s%n", "joe");

Create a Formatter and link it to a StringBuilder. Output formatted using the format() method will be appended to the StringBuilder.
StringBuilder sbuf = new StringBuilder();
Formatter fmt = new Formatter(sbuf);
fmt.format("PI = %f%n", Math.PI);
System.out.print(sbuf.toString());
// you can continue to append data to sbuf here.






===========================================================


===========================================================



===========================================================

No comments:

Post a Comment