MessageFormat.format()用法
1.java.text.Format的继承结构如下
2.MessageFormat模式
FormatElement
{ ArgumentIndex }:是从0开始的入参位置索引
{ ArgumentIndex , FormatType }
{ ArgumentIndex , FormatType , FormatStyle }
FormatType:指定使用不同的Format子类对入参进行格式化处理。值范围如下:
number:调用NumberFormat进行格式化
date:调用DateFormat进行格式化
time:调用DateFormat进行格式化
choice:调用ChoiceFormat进行格式化
FormatStyle:设置FormatType中使用的格式化样式。值范围如下:
short、medium、long、full、integer、currency、percent、SubformatPattern (子格式模式,形如#.##)
还以str为例,在这个字符串中:
1、{0}和{1,number,short}和{2,number,#.#};都属于FormatElement,0,1,2是ArgumentIndex。
2、{1,number,short}里面的number属于FormatType,short则属于FormatStyle。
3、{1,number,#.#}里面的#.#就属于子格式模式。
指定FormatType和FormatStyle是为了生成日期格式的值、不同精度的数字、百分比类型等等。
3.用法
String msg = "{0}{1}{2}{3}{4}{5}{6}{7}{8}"; Object [] array = new Object[]{"A","B","C","D","E","F","G","H","I",}; String value = MessageFormat.format(msg, array); System.out.println(value); // 输出:ABCDEFGHI
2、格式化字符串时,两个单引号才表示一个单引号,单个单引号会被省略,除非中文单引号不会被省略,如:
String value = MessageFormat.format("oh, {0} is 'a' pig", "ZhangSan"); System.out.println(value); // 输出:oh, ZhangSan is a pig
给字母a加上单引号,如:
String value = MessageFormat.format("oh, {0} is 'a' pig", "ZhangSan"); System.out.println(value); // 输出:oh, ZhangSan is a pig如果需要显示双引号要进行转移,比如:String msg = "oh, {0} is \"a\" pig";
3、单引号会使其后面的占位符均失效,导致直接输出占位符。
MessageFormat.format("{0}{1}", 1, 2); // 结果12 MessageFormat.format("'{0}{1}", 1, 2); // 结果{0}{1} MessageFormat.format("'{0}'-{1}", 1, 2); // 结果{0}-2使用双引号和两个单引号没有关系,比如
String value = MessageFormat.format("oh, ''{0}'' is a pig", "ZhangSan"); System.out.println(value); // 输出:oh, 'ZhangSan' is a pig又比如,使用子格式模式,多了一个单引号:
String value = MessageFormat.format("oh, {0,number,#.#} is good num", Double.valueOf("3.1415")); System.out.println(value); // 输出:oh, 3.1 is good num
3、无论是有引号字符串还是无引号字符串,左花括号都是不支持的,如:
String value = MessageFormat.format("oh, } is good num", Double.valueOf("3.1415")); System.out.println(value); // 输出:oh, } is good num如果使用左花括号会出现异常
String value = MessageFormat.format("oh, { is good num", Double.valueOf("3.1415")); System.out.println(value); // java.lang.IllegalArgumentException: Unmatched braces in the pattern.因此要使用到左花括号需要使用单引号配合使用
MessageFormat.format("'{'{0}}", "X-rapido"); // {X-rapido}还有一个有趣的现象,如果出现两个或2个以上左花括号,就会出现分割字符串,但是右花括号就没问题,虽然没有任何意义,实际应用我们也用不到
String value = MessageFormat.format("oh, {{ is good num", "d"); System.out.println(value); // oh, String value = MessageFormat.format("oh, }} is good num", "d"); System.out.println(value); // oh, }} is good num
关于MessageFormat.format方法:
每调用一次MessageFormat.format方法,都会新创建MessageFormat的一个实例,相当于MessageFormat只使用了一次。MessageFormat类的format方法如下:
public static String format(String pattern, Object ... arguments){ MessageFormat temp = new MessageFormat(pattern); return temp.format(arguments); }因此若要多次格式同一个模式的字符串,那么创建一个MessageFormat实例在执行格式化操作比较好些
String message = "oh, {0} is a pig"; MessageFormat messageFormat = new MessageFormat(message); Object[] array = new Object[]{"ZhangSan"}; String value = messageFormat.format(array); System.out.println(value);
MessageFormat.format()用法的更多相关文章
- MessageFormat.format用法
用法一: package gacl.request.study; import java.io.IOException; import java.text.MessageFormat; import ...
- java MessageFormat.format 用法
FormatElement: { ArgumentIndex }:是从0开始的入参位置索引. { ArgumentIndex , FormatType } { ArgumentIndex , Form ...
- String.format和MessageFormat.format的对比用法
1.MessageFormat.format import java.text.MessageFormat; /** * Created by SYJ on 2017/9/13. */ public ...
- 关于java中MessageFormat.format中单引号问题
我们知道java中可以用MessageFormat.format来格式化字符串.这个方法在我们的实际开发中经常用到,有点类似模板,这样我们就不需要用很恶心的拼接字符串了.如下面 String s1=& ...
- Java魔法堂:初探MessageFormat.format和ChoiceFormat
一.前言 刚开始从.net的转向java的时候总觉得 String.format 用得不习惯,希望格式模版会这样 {}, }$s,$s's cat.%2$s,this is %1$s's dog. . ...
- MessageFormat理解,MessageFormat.format(Object obj)方法
MessageFormat.format(Object obj)方法主要用途为拼接message信息 用法: Object[] testArgs = {new String("张三" ...
- 7. JDK拍了拍你:字符串拼接一定记得用MessageFormat#format
目录 ✍前言 版本约定 ✍正文 DateFormat:日期时间格式化 SimpleDateFormat NumberFormat:数字格式化 DecimalFormat 一.0和#的使用(最常见使用场 ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- String.format()用法
package junit.test; import java.util.Date; import java.util.Locale; import org.junit.Test; pub ...
随机推荐
- Spring for Apache Kafka @KafkaListener使用及注意事项
官方文档: https://docs.spring.io/spring-kafka/reference/html/ @KafkaListener The @KafkaListener annota ...
- portmap 和 rpc程序
Portmap 是为RPC 程序服务的. 每一个RPC server程序启动的时候要向portmap程序注册.这样portmap程序就知道这些RPC server监听在哪个端口. 而RPC clien ...
- 一个效果非常华丽的仿桌面APP,却胜似Launcher
开发Android APP的同学是否对于Launcher实现的绚丽效果而痴迷呢?什么.连Android Launcher是什么都不知道.好吧,拿起侬的手机.在解锁后的首页界面上左右滑动滑动,体验体验, ...
- WPF学习笔记——没有前途的WPF
看上去,WPF比silverlight有前途一点.毕竟,微软还没有宣布,WPF停止更新. 但我怀疑,不久的将来,WPF也会步其子集silverlight的后尘,要么不再出后续版本,要么向HTML5 + ...
- git fetch批处理,遍历一个文件夹下的所有子目录,执行git fetch --all
echo off for /d %%i in (*) do ( echo %%i cd %%i git fetch --all cd .. ) 判断子目录是否有.git文件夹 echo off for ...
- linux端口号与PID的互相查询
最近用linux在玩Tomcat,启动的时候总是会报错(8080/8009/8005) 于是整理了一下网上零乱的查看PID和端口的命令,以备记录. 1.由端口号查询PID号 首先myeclipse报错 ...
- UESTC--1252--24点游戏(dfs)
24点游戏 Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Submit Status ...
- Gym - 101981J The 2018 ICPC Asia Nanjing Regional Contest J.Prime Game 计数
题面 题意:1e6的数组(1<a[i]<1e6), mul (l,r) =l × (l+1) ×...× r, fac(l,r) 代表 mul(l,r) 中不同素因子的个数,求s ...
- spark作业运行过程之--DAGScheduler
DAGScheduler--stage划分和创建以及stage的提交 本篇,我会从一次spark作业的运行为切入点,将spark运行过程中涉及到的各个步骤,包括DAG图的划分,任务集的创建,资源分配, ...
- c语言中struct的初始化
C++中的struct已经和class一样,可以用构造函数初始化. C语言中的struct怎么初始化呢? typedef struct _TEST_T { int i; c ...