String toString()
描述 (Description)
java.util.Formatter.toString()方法返回在输出目标上调用toString()的结果。
声明 (Declaration)
以下是java.util.Formatter.toString()方法的声明
public String toString()
参数 (Parameters)
NA
返回值 (Return Value)
此方法返回在输出的目标上调用toString()的结果。
异常 (Exception)
FormatterClosedException - 如果已通过调用其close()方法关闭此formatter
例子 (Example)
以下示例显示了java.util.Formatter.toString()方法的用法。
package com.iowiki;
import java.util.Formatter;
import java.util.Locale;
public class FormatterDemo {
public static void main(String[] args) {
// create a new formatter
StringBuffer buffer = new StringBuffer();
Formatter formatter = new Formatter(buffer, Locale.US);
// format a new string
String name = "World";
formatter.format("Hello %s !", name);
// print the formatted string with default locale
System.out.println("" + formatter);
// print the formatter as a string
System.out.println("" + formatter.toString());
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Hello World !
Hello World !