Java Formatter format() 方法



描述

Java 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() 方法关闭。

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

使用 Formatter 示例格式化包含变量的字符串

以下示例演示了如何使用 Java Formatter format(Locale, String, Object) 方法使用格式化程序格式化字符串。我们创建了一个带有 StringBuffer 和区域设置的格式化程序对象。Formatter 用于使用 format() 方法打印字符串。

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 
      System.out.println(formatter);
	  
      formatter.close();
   }
}

输出

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

Hello World !

使用 Formatter 示例格式化包含两个变量的字符串

以下示例演示了如何使用 Java Formatter format(Locale, String, Object, Object) 方法使用格式化程序格式化字符串。我们创建了一个带有 StringBuffer 和区域设置的格式化程序对象。Formatter 用于使用 format() 方法打印字符串。

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 = "Robert";
      int rollNo = 10;
      formatter.format(Locale.US, "Name = %s, RollNo = %d", name, rollNo);

      // print the formatted string 
      System.out.println(formatter);
      
      formatter.close();
   }
}

输出

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

Name = Robert, RollNo = 10

使用 Formatter 示例格式化包含三个变量的字符串

以下示例演示了如何使用 Java Formatter format(Locale, String, Object, Object, Object) 方法使用格式化程序格式化字符串。我们创建了一个带有 StringBuffer 和区域设置的格式化程序对象。Formatter 用于使用 format() 方法打印字符串。

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 = "Robert";
      int rollNo = 10;
      String section = "A";
      formatter.format(Locale.US,"Name = %s, RollNo = %d, Section = %s", name, rollNo, section);

      // print the formatted string 
      System.out.println(formatter);
      
      formatter.close();
   }
}

输出

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

Name = Robert, RollNo = 10, Section = A
java_util_formatter.htm
广告

© . All rights reserved.