Java Format - Java Formatter.toString()


Formatter.toString() has the following syntax.

public String toString()


In the following code shows how to use Formatter.toString() method.

import java.util.Formatter;
import java.util.Locale;
/*w  w w.  ja  va 2  s  .c  om*/
public class Main {

   public static void main(String[] args) {

      StringBuffer buffer = new StringBuffer();
      Formatter formatter = new Formatter(buffer, Locale.US);

      // format a new string
      String name = "from";
      formatter.format("Hello %s !", name);

      // print the formatted string with default locale

      // print the formatter as a string

The code above generates the following result.