printf格式化字符串_Java printf()–将格式化的字符串打印到控制台

printf格式化字符串

We’ve already discussed Java println() method in a previous tutorial. Today, we’ll discuss the printf() method and its various implementations in detail. Ready. Get. Set. Go!

在上一教程中,我们已经讨论过Java println()方法。 今天,我们将详细讨论printf()方法及其各种实现。 准备。 得到。 组。 走!

Java printf()
  • printf()方法不仅在C语言中,而且在Java中。
  • 此方法属于PrintStream类。
  • 它用于使用各种格式说明符打印格式化的字符串。

句法

(Java printf()
  • printf() method is not only there in C, but also in Java.
  • This method belongs to the PrintStream class.
  • It’s used to print formatted strings using various format specifiers.

Syntax

)

Following are the syntaxes available for the printf() method:

以下是可用于printf()方法的语法:

System.out.printf(string);
System.out.printf(format, arguments);
System.out.printf(locale, format, arguments);

The first one does not do any formatting though and it’s like the println() method.

第一个虽然没有进行任何格式化,但是它类似于println()方法。

System.out.format() is same as System.out.format()System.out.printf() method. System.out.printf()方法相同。

String.format()和System.out.printf()之间的区别 (Difference between String.format() and System.out.printf())

  1. String.format() returns a formatted string. System.out.printf() also prints a formatted string to the console.

    String.format()返回格式化的字符串。 System.out.printf()也将格式化的字符串打印到控制台。
  2. printf() uses the java.util.Formatter class to parse the format string and generate the output.

    printf()使用java.util.Formatter类解析格式字符串并生成输出。

格式说明符 (Format Specifiers)

Let’s look at the available format specifiers available for printf:

让我们看一下可用于printf的可用格式说明符:

  • %c character

    %c个字符
  • %d decimal (integer) number (base 10)

    %d个十进制(整数)数字(以10为底)
  • %e exponential floating-point number

    %e指数浮点数
  • %f floating-point number

    %f个浮点数
  • %i integer (base 10)

    %i整数(以10为底)
  • %o octal number (base 8)

    %o八进制数(以8为底)
  • %s String

    %s字串
  • %u unsigned decimal (integer) number

    %u无符号十进制(整数)数字
  • %x number in hexadecimal (base 16)

    以十六进制表示的%x个数字(以16为基)
  • %t formats date/time

    %t格式化日期/时间
  • %% print a percent sign

    %%打印百分号
  • \% print a percent sign

    \%打印百分号

Note: %n or \n are used as line separators in printf().

注意%n\ n用作printf()行分隔符。

转义字符 (Escape Characters)

Following are the escape characters available in printf():

以下是printf()可用的转义字符:

  • \b backspace

    \ b退格键
  • \f next line first character starts to the right of current line last character

    \ f下一行第一个字符从当前行最后一个字符的右边开始
  • \n newline

    \ n换行符
  • \r carriage return

    \ r回车
  • \t tab

    \ t标签
  • \\ backslash

    \\反斜杠

格式说明符完整语法 (Format Specifiers Full Syntax)

Let’s look at the full syntax of format specifiers with the extended set:

让我们看一下带有扩展集的格式说明符的完整语法:

%<flags><width><.precision>specifier

flags can be set as + for right-aligning, and – for left-aligning.

可以将标志设置为+(用于右对齐)和–(用于左对齐)。

Next, fire up your Jshell and start using printf()!

接下来,启动您的Jshell并开始使用printf()

数字格式 (Number Formatting)

Here’s an example:

这是一个例子:

|  Welcome to JShell -- Version 12.0.1
|  For an introduction type: /help intro

jshell> int x = 10
x ==> 10

jshell> System.out.printf("Formatted output is: %d %d%n", x, -x)
Formatted output is: 10 -10

Let’s use some precision formatting:

让我们使用一些精确格式:

jshell> float y = 2.28f
y ==> 2.28

jshell> System.out.printf("Precision formatting upto 4 decimal places %.4f\n",y)

Precision formatting upto 4 decimal places 2.2800

jshell> float z = 3.147293165f
z ==> 3.147293

jshell> System.out.printf("Precision formatting upto 2 decimal places %.2f\n",z)

Precision formatting upto 2 decimal places 3.15

As you can see it rounds off to the next decimal in the second case.

如您所见,在第二种情况下,它会四舍五入到下一个小数。

宽度说明符,对齐,用零填充 (Width Specifier, Aligning, Fill With Zeros)

In this section, we’ll see three examples for each of these:

在本节中,我们将为每个示例看到三个示例:

jshell> System.out.printf("'%5.2f'%n", 2.28);
' 2.28'

As you can see the width specifier allocates 5 characters width. The content is right aligned by default.

如您所见,宽度说明符分配5个字符的宽度。 默认情况下,内容右对齐。

Filling with zeros

用零填充

Empty spaces to the left of the first character can be filled with zeroes as shown below:

第一个字符左侧的空白可以用零填充,如下所示:

jshell> System.out.printf("'%05.2f'%n", 2.28);
'02.28'

jshell> System.out.printf("'%010.2f'%n", 2.28);
'0000002.28'

jshell> System.out.printf("'%010.2f'%n", -2.28);
'-000002.28'

jshell> System.out.printf("'%010.2f'%n", 1234567.89);
'1234567.89'

jshell> System.out.printf("'%010.2f'%n", -1234567.89);
'-1234567.89'

Aligning
By default, it is a + which means right aligned.

对齐
默认情况下,它是一个+,表示右对齐。

jshell> System.out.printf("'%10.2f'%n", 2.28);
'      2.28'

The following code, aligns to the left:

下面的代码向左对齐:

jshell> System.out.printf("'%-10.2f'%n", 2.28);
'2.28      '

Using Comma and Locale:

使用逗号和语言环境:

jshell> System.out.printf(Locale.US, "%,d %n", 5000);
5,000

字符串,布尔格式 (String, Boolean formatting)

Let’s look at String formatting with a few basic examples:

让我们看一下一些基本示例的字符串格式:

jshell> System.out.printf("%s %s!%n","Hello","World");
Hello World!
jshell> System.out.printf("%s\f%s!%n","Hello","World!");
Hello
     World!!
jshell> System.out.printf("%s\\%s!%n","Hello","World!");
Hello\World!!

Uppercase:

大写:

jshell> System.out.printf("%s %S!%n","Hello","World");
Hello WORLD!

Boolean formatting examples are given below:

布尔格式示例如下:

jshell> System.out.printf("%b%n", false);
false

jshell> System.out.printf("%b%n", 0.5);
true

jshell> System.out.printf("%b%n", "false");
true

时间格式 (Time Formatting)

‘H’, ‘M’, ‘S’ – Hours, Minutes, Seconds
‘L’, ‘N’ – to represent the time in milliseconds and nanoseconds accordingly
‘p’ – AM/PM
‘z’ – prints out the difference from GMT.

'H', 'M', 'S' –小时,分钟,秒
'L', 'N' –分别表示毫秒和纳秒的时间
'p'上午/下午
'z'打印出与GMT的区别。

jshell> Date date = new Date();
date ==> Fri Apr 19 02:15:36 IST 2019

jshell> System.out.printf("%tT%n", date);
02:15:36

jshell> System.out.printf("H : %tH, M: %tM, S: %tS%n",date,date,date)
H : 02, M: 15, S: 36

The latter one requires many arguments which are the same.
Instead, we can replace them with a single one:

后者需要许多相同的论点。
相反,我们可以将它们替换为一个:

jshell> System.out.printf("%1$tH:%1$tM:%1$tS %1$Tp GMT %1$tz  %n", date)
02:15:36 AM GMT +0530

日期格式 (Date Formatting)

Date formatting has the following special characters

日期格式具有以下特殊字符

A/a – Full day/Abbreviated day
B/b – Full month/Abbreviated month
d – formats a two-digit day of the month
m – formats a two-digit month
Y – Full year/Last two digits of the Year
j – Day of the year

A / a –全天/缩写天
B / b –整月/缩写月
d –格式化一个两位数的日期
m –格式化两位数的月份
Y –全年/年份的最后两位数字
j –一年中的一天

jshell> System.out.printf("%s %tB %<te, %<tY", "Current date: ", date);
Current date:  April 19, 2019

jshell> System.out.printf("%1$td.%1$tm.%1$ty %n", date);
19.04.19

jshell> System.out.printf("%s %tb %<te, %<ty", "Current date: ", date);
Current date:  Apr 19, 19

结论 (Conclusion)

In this tutorial, we discussed the various types of formatting possible using printf() method.

在本教程中,我们讨论了使用printf()方法可能进行的各种格式化。

翻译自: https://www.journaldev.com/28692/java-printf-method

printf格式化字符串

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JAVA 范例大全 光盘 资源 书籍目录: 前言. 第1章 开发环境搭建 1 实例1 下载、安装并配置JDK 1 实例2 第一个Java程序 3 实例3 在Eclipse中创建第一个Java程序 4 常见问题 javac不是内部或者外部命令 6 常见问题 找不到类文件 6 常见问题 语法错误 7 第2章 Java基础语法 9 实例4 变量和常量 9 实例5 基本数据类型转换 10 实例6 操作多种运算符 12 实例7 不同数制间的转换 17 实例8 多种方式实现阶乘的算法 20 第3章 流程控制语句 23 实例9 打印任一年日历 23 实例10 控制台输出几何图形 26 实例11 杨辉三角 28 实例12 拜访过程(break和continue) 29 常见问题 for循环初始化问题 31 .第4章 数组 32 实例13 一维数组复制、插入和合并 32 实例14 数组排序 35 实例15 数组搜索 37 实例16 去掉数组重复数字 39 实例17 求质数(素数) 41 实例18 矩阵的加减和转置 43 实例19 数组实现顺序栈与队列 46 实例20 Arrays数组的应用 50 第5章 面向对象设计 54 实例21 图形面积与周长(抽象类) 54 实例22 宠物结婚(封装) 56 实例23 一个盒子(继承) 58 实例24 学生的生活(多态) 60 实例25 员工薪资(接口) 62 实例26 我的类型(instanceof运算符) 66 实例27 匿名内部类 68 实例28 静态内部类 70 实例29 成员内部类 71 实例30 局部内部类 73 实例31 单例模式(Singleton) 75 实例33 开车(简单工厂模式) 77 实例33 旅游(工厂方法模式) 79 实例34 花园布局(抽象工厂模式) 80 实例35 几何图形(适配器模式) 82 第6章 字符串 85 实例36 获取字符串长度 85 实例37 比较字符串 86 实例38 Java字符串与文件的互转 88 实例39 截取带汉字的字符串 91 实例40 替换字符串中的部分字符 92 实例41 Java字符串之密码加密 93 实例42 正则表达式验证字符串 95 第7章 Java常用类 98 实例43 数字的舍入 98 实例44 转换数字的进制 101 实例45 随机数 103 实例46 Java Applet绘制心形曲线 106 实例47 简单的计算器 107 实例48 日历和日期 118 实例49 Java编制的时钟 121 实例50 简单的日历 124 实例51 内存管理 130 实例52 利用currentTimeMillis()计算程序执行的时间 131 实例53 利用exit()退出虚拟机 132 实例54 获取和设置环境属性 133 实例55 利用换底公式求任意对数值 135 实例56 使用取整函数 135 实例57 利用GregorianCalendar输出日历 136 实例58 Formatter类的简单使用 138 实例59 Pattern类的使用 140 实例60 匹配方法的使用 141 实例61 替换方法的使用 142 实例62 检验E-mail的合法性 143 第8章 集合 146 实例63 谁养鱼(运用ArrayList) 146 实例64 查看书目(运用Iterator) 153 实例65 操作元素(运用Vector) 155 实例66 栈和队列(运用LinkedList) 157 实例67 电视频道(运用集的相关类) 162 实例68 植物种类(运用映射的相关类) 165 实例69 不重复的随机数序列 168 实例70 读写Properties文件 170 实例71 配置Properties带附件发送邮件 175 实例72 资源国际化(Properties) 179 常见问题 读取Properties文件出现中文乱码 182 第9章 Java异常处理与反射机制 183 实例73 运用throws、throw、try与catch 183 实例74 throws声明异常的实例 185 实例75 自定义异常类 187 实例76 使用finally避免资源漏洞 189 实例77 反射机制 191 第10章 I/O及文件操作 196 实例78 创建文件和目录 196 实例79 查找文件 199 实例80 删除文件夹和文件 201 实例81 文件复制与移动 204 实例82 多种方式读取文件内容 209 实例83 多种方式写文件 213 实例84 随机访问文件 216 实例85 追加文件内容 219 实例86 文件锁定 220 实例87 分割与合并文件 222 实例88 序列化和反序列化 226 实例89 Zip格式压缩、解压缩文件 228 实例90 从Jar中读取文本 232 实例91 流标记分割和统计字符串 234 实例92 Java操作Excel文件 237 第11章 Java高级特性 245 实例93 自动装箱与拆箱 245 实例94 for/in循环 247 实例95 参数不确定(可变长参数) 249 实例96 方法改变(协变式返回类型) 251 实例97 静态导入 252 实例98 动物搭配(泛型) 253 实例99 人员信息(枚举类型) 256 实例100 printf()用法 260 实例101 使用ProcessBuilder调用外部命令 263 实例102 监控管理虚拟机 267 第12章 XML开发.. 273 实例103 HTML文件转成XML文件 273 实例104 XML文件转成HTML文件 275 实例105 DOM4j解析XML文件 278 实例106 JDOM解析XML文件 285 实例107 DOM解析XML文件 292 实例108 SAX解析XML文件 297 实例109 W3C解析XML文件 301 第13章 Java网络编程 306 实例110 获取IP地址和域名 306 实例111 获取网络资源(URL) 308 实例112 FTP文件传输模拟 311 实例113 自制浏览器 316 实例114 点对点通信(Socket基于TCP协议) 323 实例115 点对面通信(Socket基于TCP/IP协议) 327 实例116 多线程断点续传(基于HTTP) 332 实例117 代理服务器的实现 340 实例118 IP多点传送(基于UDP的C/S) 345 第14章 线程 350 实例119 启动和停止线程 350 实例120 多线程同步方法 352 实例121 取钱存钱(线程同步互斥) 354 实例122 谁唤醒了我(线程沉睡和唤醒) 357 实例123 让步传文件(线程让步) 359 实例124 爱子(守护线程) 361 实例125 有始有终(线程连接) 363 实例126 模拟下载文件(线程等待和通报) 365 实例127 家族等级(线程优先级) 368 实例128 定时器(Timer) 371 实例129 没法吃饭(死锁) 372 实例130 方便吃饭(解决死锁) 375 实例131 查看JVM中所有的线程和线程组 376 实例132 执行任务(线程池) 378 实例133 碰撞的球(多线程) 382 实例134 钟表(多线程) 387 实例135 模拟生产者与消费者 392 实例136 仿迅雷下载文件 396 第15章 图形编程 403 实例137 多变的按钮 403 实例138 自制对话框 405 实例139 模仿QQ空间的电子相册 409 实例140 会动的七彩文字 413 实例141 模仿3D渐层效果 416 实例142 模仿QQ空间的欢迎动画 418 实例143 百叶窗效果 420 实例144 闪电效果 425 实例145 模拟放大镜效果 430 实例146 水面倒影 432 实例147 美丽的烟花 435 实例148 开窗游戏 439 第16章 Java安全 445 实例149 一个简单的加密和解密程序—凯撒密码 445 实例150 创建对称密钥 447 实例151 CBC方式的加密 448 实例152 CBC方式的解密 450 实例153 计算消息摘要 451 实例154 使用消息摘要保存口令 452 实例155 使用消息摘要验证口令 454 实例156 攻击消息摘要保存的口令 455 实例157 使用加盐技术防范字典式攻击 457 实例158 输入流的加密 459 实例159 输入流的解密 460 实例160 输出流的加密 461 实例161 输出流的解密 463 实例162 RSA算法进行加密 464 实例163 RSA算法进行解密 466 实例164 创建DH共享密钥 468 实例165 用公钥计算消息摘要的验证码 472 实例166 利用DES加密/解密 474 第17章 Java多媒体 495 实例167 幸运52(Applet) 495 实例168 三维弹球游戏(Java 3D) 498 实例169 贪吃的小蛇 502 实例170 有趣的拼图游戏 510 实例171 滚动的文字 517 实例172 简单的GIF动画效果 519 实例173 简单的声音播放 522 实例174 多媒体播放器 526 实例175 有趣的采钻游戏 532 第18章 Java Mail 543 实例176 发送邮件 543 实例177 发送附件邮件 549 实例178 一对多的发送方式 552 实例179 接收邮件 556 实例180 删除邮件 572 实例181 利用Java API发送E-mail 574 第19章 数据库技术 583 实例182 连接各种数据库 583 实例183 创建表结构 587 实例184 表数据的基本操作 590 实例185 批处理 593 实例186 事务处理 596 实例187 Applet连接数据库 600 实例188 简单的JDBC连接 608 实例189 RowSet接口 610 实例190 调用存储过程 617 实例191 图片文件存入数据库 620 实例192 数据库图片的输出 622 实例193 利用console控制台运行类中的汉字处理方案 624 实例194 Servlet中的汉字处理方案 625 实例195 JSP中的汉字处理方案 627 实例196 Tomcat连接池的配置 629 实例197 MySQL数据库的分页形式 633 实例198 连接ODBC数据库的Apple程序 640 第20章 JSP/Servlet技术 644 实例199 JSP与Servlet之间的跳转 644 实例200 简单的JSP多人聊天室 653 实例201 Servlet生成的动态图片 658 实例202 简单的JSP上传文件 661 实例203 用Servlet获取Web服务器信息 666 实例204 可选择的图形验证码 670 实例205 简单的页面注册 675 实例206 用Servlet实现分页查看数据库 686 实例207 简单的BBS论坛 697 第21章 Java程序综合案例:教务处管理系统 705 21.1 登录界面的设计与代码实现 705 21.2 功能选择界面的设计 708 21.3 学生信息系统界面的设计 716 21.4 教师信息系统界面的设计 727 21.5 领导信息系统界面的设计... 738
目录 1 正文 3 一、 C++的string的使用 3 1.1 C++ string简介 3 1.2 string的成员 3 1.2.1 append 3 1.2.2 assign 4 1.2.3 at 4 1.2.4 begin 5 1.2.5 c_str 5 1.2.6 capacity 5 1.2.7 clear 6 1.2.8 compare 6 1.2.9 copy 6 1.2.10 _Copy_s 6 1.2.11 data 6 1.2.12 empty 6 1.2.13 end 6 1.2.14 erase 6 1.2.15 find 6 1.2.16 find_first_not_of 7 1.2.17 find_first_of 8 1.2.18 find_last_not_of 8 1.2.19 find_last_of 8 1.2.20 get_allocator 8 1.2.21 insert 8 1.2.22 length 8 1.2.23 max_size 8 1.2.24 push_back 8 1.2.25 rbegin 8 1.2.26 rend 8 1.2.27 replace 8 1.2.28 reserve 10 1.2.29 resize 11 1.2.30 rfind 11 1.2.31 size 11 1.2.32 substr 11 1.2.33 swap 11 1.3 string的构造 11 1.4 string的重载运算符 12 1.5 string与algorithm相结合的使用 12 1.5.1 string与remove 12 1.5.2 string与unique、sort 12 1.5.3 string与search 12 1.5.4 string和find、find_if 13 1.5.5 string与copy、copy_if 13 1.5.6 string与count、count_if 14 1.6 string与wstring 14 1.6.1 简介 14 1.6.2 wstring实例 15 1.6.3 wstring与控制台 15 1.6.4 string与wstring的相互转换 16 1.7 string与C++流 21 1.7.1 C++流简介 21 1.7.2 string与iostream、fstream 21 1.8 格式化字符串 22 1.8.1 简单常用的C方法 22 1.8.2 boost的方法 22 1.9 string与CString 23 二、 boost字符串算法库 23 2.1 boost字符串算法库导论 23 2.1.1 boost.algorithm.string是什么? 23 2.1.2 相关 23 2.1.3 boost.range导论 23 2.1.4 boost.regex导论 23 2.1.5 boost.algorithm.string的DNA 24 2.2 boost字符串算法解密 24 2.2.1 修剪(trim.hpp) 24 2.2.2 转换(case_conv.hpp) 26 2.2.3 判断式、断言函数(predicate.hpp)【Predicates】 27 2.2.4 查找 28 2.2.5 删除和替换 29 2.2.6 分割和组合 31 2.2.7 其它 32 三、 C字符串 32 3.1 C字符串常用算法 32 3.1.1 strcpy wcscpy 32 3.1.2 strcat wcscat 32 3.1.3 strchr wcschr 32 3.1.4 strcmp wcscmp 33 3.1.5 stricmp wcsicmp 33 3.1.6 strlen wcslen 33 3.1.7 strlwr/_strlwr wcslwr/_wcslwr 33 3.1.8 strncat wcsncat 33 3.1.9 strcspn wcscspn 33 3.1.10 strdup/_strdup wcsdup/_wcsdup 34 3.1.11 strncpy wcsncpy 34 3.1.12 strpbrk wcspbrk 35 3.1.13 strrev/_strrev wcsrev/_wcsrev 35 3.1.14 strset/_strset/_strset_l wcsset/_wcsset/_wcsset_l 35 3.1.15 strstr/wcsstr 35 3.1.16 strtok/wcstok 36 3.1.17 strupr/_strupr wcsupr/_wcsupr 36 3.2 更安全的C字符串函数 36 3.2.1 简述 36 3.2.2 简单实例 36 3.2.3 定制 38 3.2.4 兼容 41 3.3 通用字串函数 47 3.3.1 简述 47 3.3.2 简单实例 47 3.3.3 映射表 48 3.4 API级的字符串处理 48 3.4.1 简述 48 3.4.2 旧的API 48 3.4.3 Shell字符串函数 48 3.4.4 新的安全版字符串处理API 48 四、 C++字符串使用的建议 51 附录1:参考资料: 51 附录2: MSSTL中basic_string的部分源码解读 51 2.1 string的allocator 51 2.1.1 Allocate和Deallocate 51 2.1.2 allocator的泛型实现 52 2.1.3 string与char_traits 54 2.1.4 以char和wchar_t特化char_traits 56 附录3:Boost.Format中文文档 57 2.1 大纲 57 2.2 它是如何工作的 57 2.3语法 58 2.3.1 boost::format( format-string ) % arg1 % arg2 % ... % argN 58 2.3.2 printf 格式化规则 59 2.3.3 新的格式规则 60 附录4 TCHAR.h 映射表 60

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值