全屏
java.util.Formatter.toString()方法返回调用toString()方法在目的地的输出结果。
声明
以下是java.util.Formatter.toString()方法的声明public String toString()
参数NA
返回值
此方法返回的目的地为输出调用的toString()的结果
异常FormatterClosedException -- 如果该格式已经被关闭通过调用它的close()方法
例子
下面的示例演示java.util.Formatter.toString()方法的用法。package cn.sxt;
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 !
分享到:
0评论