Java.util.Formatter.format()


描述

该java.util.Formatter.format(Locale l,String format,Object... args)方法的格式化字符串使用指定的语言环境参数此对象的目标,格式字符串和。

声明

以下是java.util.Formatter.format()方法的声明

public Formatter format(Locale l,String format,Object... args)

参数

l - 格式化期间要应用的语言环境。如果l为null,则不应用本地化。这不会更改此对象在构造期间设置的区域设置。

format - 格式字符串语法中描述的格式字符串。

args - 格式字符串中格式说明符引用的参数。如果参数多于格式说明符,则忽略额外参数。参数的最大数量受Java虚拟机规范定义的Java数组的最大维数限制

返回值

此方法返回此格式化程序

异常

FormatterClosedException - 如果已通过调用其close()方法关闭此formatter

IllegalFormatException - 如果格式字符串包含非法语法,格式说明符与给定参数不兼容,给定格式字符串的参数不足或其他非法条件。

实例

以下示例显示了java.util.Formatter.flush()方法的用法。

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(Locale.US,"Hello %s !", name);

      // print the formatted string with specified locale
      System.out.println("" + formatter + " " + formatter.locale());
   }
}

让我们编译并运行上面的程序,这将产生以下结果

Hello World ! en_US