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

Java Formatter format()方法及示例相关推荐

  1. java formatter 填充_Java Formatter format()方法及示例

    格式化程序类format()方法 语法:public Formatter format(Locale lo, String frmt, Object... args); public Formatte ...

  2. java的formatter_java.util.Formatter.format()方法实例

    全屏 java.util.Formatter.format(String format,Object... args) 方法写入一个格式化字符串使用指定格式字符串和参数此对象的目标.所使用的语言环境是 ...

  3. Java IOUtils.copy方法代码示例(亲测)

    本文整理汇总了Java中org.apache.commons.io.IOUtils.copy方法的典型用法代码示例.如果您正苦于以下问题:Java IOUtils.copy方法的具体用法?Java I ...

  4. java百分号的用法,Java String.format()方法中使用百分号'%'_栗子教程

    下面这个例子演示了如何在String.format方法中显示一个百分号. 像下面这样直接调用String.format("%d%", 100)来显示百分号,将会抛出java.uti ...

  5. 记录对String.format(Formatter().format())方法的总结

    String.format其实是调用的Formatter.format: public static String format(String format, Object... args) {ret ...

  6. JAVA String.format 方法使用介绍

    在JDK1.5中,String类增加了静态方法format(String format, Objects... args),format(Local l ,String format, Objects ...

  7. JAVA String format 方法使用介绍

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 在JDK ...

  8. Java序列化魔术方法及其示例使用

    在上一篇文章中, 您需要了解有关Java序列化的所有知识 ,我们讨论了如何通过实现Java序列化来启用类的可序列化性. Serializable接口. 如果我们的类未实现Serializable接口, ...

  9. java user directory,Java ProcessBuilder directory()方法与示例

    语法:public File directory (); public ProcessBuilder directory (File dir); ProcessBuilder类directory()方 ...

最新文章

  1. Silverlight C# 游戏开发:Silverlight开发环境
  2. mysql快速迁移数据sql_使用Navicat Premium工具快速迁移数据方法 适用于mysql及mssql数据库...
  3. php接口调用教程,php接口调用
  4. matlab 数据拟合
  5. (JAVA学习笔记) static关键字详解
  6. linux 漏洞数量,Debian Linux被列为过去20年漏洞数量最多的操作系统
  7. Linux使用技巧9--用dpkg管理你的软件
  8. LoadRunner几个重要的概念:事务、集合点、思考时间
  9. day19 java数组的常用算法和排序
  10. #if defined和#if !defined的含义
  11. Linq学习比较第2篇
  12. VSTO简介及简单使用
  13. cmd命令:批量创建文件夹
  14. React+Antd兼容ie浏览器,360安全浏览器兼容模式
  15. 王师傅玩的c语言小游戏,王师傅竟然背着老婆和男人做出这种事...最好玩的炉石冒险活动来了!...
  16. 使用Envato Elements Sketch插件立即创建惊人的原型
  17. sendfile优化文件拷贝
  18. Java实现多条相同数据合并为一条数据
  19. Code Representation方面的Empirical Studies
  20. linux档案权限655,Linux 的档案权限与目录配置

热门文章

  1. svd降维 python案例_SVD(奇异值分解)Python实现
  2. rocketmq 消息指定_SpringBoot 整合 RocketMQ 如何实现消息生产消费?
  3. https开头的网址是什么意思_我想打这个面试官,他给我挖坑,问我:URI中的 “//” 有什么用?...
  4. 你用计算机做过什么事情,他偷偷干了什么?(电脑)
  5. Redis(十三):Redis实现乐观锁
  6. Angular之ngx-permissions的管理权限
  7. 2016: C语言实验——打印金字塔
  8. SharePoint v3:忘掉模拟用户Impersonate,SPSecurity.RunWithElevatedPrivileges来了
  9. 深度学习在搜索业务中的探索与实践 1
  10. PC,移动端H5实现实现小球加入购物车效果