當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.util.Formatter.toString()用法及代碼示例


描述

這個java.util.Formatter.toString()方法返回在輸出目標上調用 toString() 的結果。

聲明

以下是聲明java.util.Formatter.toString()方法

public String toString()

參數

NA

返回值

此方法返回在輸出目標上調用 toString() 的結果。

異常

FormatterClosedExceptionâˆ' 如果此格式化程序已通過調用其 close() 方法關閉

示例

下麵的例子展示了 java.util.Formatter.toString() 方法的用法。

package com.tutorialspoint;

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 !

相關用法


注:本文由純淨天空篩選整理自 Java.util.Formatter.toString() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。