Java Formatter format()方法及示例

Formatter类format()方法 (Formatter Class format() method)

Syntax:

句法:

    public Formatter format(Locale lo, String frmt, Object... args);
    public Formatter format(String frmt, Object... args);

  • format() method is available in java.util package.

    format()方法在java.util包中可用。

  • format(Locale lo, String frmt, Object… args) method is used to return this formatter which writes formatted string to this object by the given locale(lo), string format (frmt) and the given Object argument.

    format(Locale lo,String frmt,Object…args)方法用于返回此格式化程序,该格式化程序通过给定的locale(lo),字符串格式(frmt)和给定的Object参数将格式化的字符串写入此对象。

  • format(String frmt, Object... args) method is used to return this formatter that writes formatted string to this object by using the given string format (frmt) and Object arguments.

    format(String frmt,Object ... args)方法用于返回此格式化程序,该格式化程序通过使用给定的字符串格式(frmt)和Object参数将格式化后的字符串写入此对象。

  • These are the non-static methods, it is accessible with the class object and if we try to access the method with the class name then we will get an error.

    这些是非静态方法,可通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • These methods may throw an exception at the time of returning Formatter.

    这些方法在返回Formatter时可能会引发异常。

    • FormatterClosedException: This exception throws when this formatter close by calling it's close().FormatterClosedException :当此格式化程序通过调用其close()关闭时,将引发此异常。
    • IllegalArgumentException: This exception when any one of the given argument is illegal or invalid format.IllegalArgumentException :当给定参数中的任何一个为非法或无效格式时,此异常。

Parameter(s):

参数:

  • In the first case, format(Locale lo, String frmt, Object... args),

    在第一种情况下, format(Locale lo,String frmt,Object ... args)

    • Locale lo – represents the locale to implement during formatting.
    • 语言环境lo –表示格式化期间要实现的语言环境。
    • String frmt – represents the format string.
    • 字符串frmt –表示格式字符串。
    • Object... args – represents the arguments linked by the format specifies in the format string.
    • Object ... args –表示由格式字符串中指定的格式链接的参数。
  • In the first case, format(String frmt, Object... args),

    在第一种情况下, format(String frmt,Object ... args)

    • String frmt – represents the format string.
    • 字符串frmt –表示格式字符串。
    • Object... args – represents the arguments linked by the format specifies in the format string.
    • Object ... args –表示由格式字符串中指定的格式链接的参数。

Return value:

返回值:

The return type of this method is Formatter, it returns this Formatter object.

此方法的返回类型为Formatter ,它返回此Formatter对象。

Example:

例:

// Java program is to demonstrate the example of
// format() method of Formatter

import java.util.*;

public class FormatOfFormatter {
    public static void main(String[] args) {
        // Instantiates a StringBuffer and Formmatter object
        StringBuffer sb = new StringBuffer();
        Formatter formatt = new Formatter(sb, Locale.UK);

        // By using format(locale,format,Object...) method is
        // to format a string with the given locale
        formatt.format(Locale.UK, "Hi %s !", "IncludeHelp");

        // Display Formatted String
        System.out.println(formatt);

        // By using format(format,Object...) method is
        // to format a string with the default locale
        formatt.format("Hi %s !", "IncludeHelp");

        // Display Formatted String
        System.out.println(formatt);
    }
}

Output

输出量

Hi IncludeHelp !
Hi IncludeHelp !Hi IncludeHelp !


翻译自: https://www.includehelp.com/java/formatter-format-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值