MessageFormat用法
java.text.MessageFormat
作用:MessageFormat 获取一组对象,格式化这些对象,然后将格式化后的字符串插入到模式中的适当位置。

MessageFormat模式:

FormatElement:
{ArgumentIndex}:是从0开始的入参位置索引
{ArgumentIndex,FormatType}
{ArgumentIndex,FormatType,FormatStyle}

FormatType:指定使用不同的Format子类对参数进行格式化处理。值范围如下:
number:调用NumberFat进行格式化
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是为了生成日期格式的值、不同精度的数字、百分比类型等等。

package cn.itcast.bookstore.user.domain;

import java.text.MessageFormat;

import org.junit.Test;
/**
* MessageFormat用法
* @author Administrator
*
*/
public class Demo {
/*
java.text.MessageFormat
作用:MessageFormat 获取一组对象,格式化这些对象,然后将格式化后的字符串插入到模式中的适当位置。
*/
/*
* ArgumentIndex必须是非负整数,它的个数不只限制于0~9这10个
*/
@Test
public void fun3(){
String msg="{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}";
Object[] array=new Object[]{"A","B","C","D","E","F","G","H","I","J","K","L","M"};
String value=MessageFormat.format(msg, array);
System.out.println(value);
} /*
* 替换的元素和用来替换的下标是一一对应的
*/
@Test
public void fun4(){
String msg="{0}{1}{2}{3}{4}";
Object[] array=new Object[]{"A","B","C","D","E","F","G"};
Object[] array1=new Object[]{"A","B"};
String value=MessageFormat.format(msg, array);
String value1=MessageFormat.format(msg, array1);
System.out.println(value);
System.out.println(value1);
} /*
* 格式化字符串时,两个单引号才表示一个单引号,单个单引号会被省略(英文单引号)
*/
@Test
public void fun5(){
String value=MessageFormat.format("oh,{0} is a 'pig' ", "zhangSan");
String value1=MessageFormat.format("oh,{0} is a ''pig'' ", "zhangSan");
System.out.println(value);
System.out.println(value1);
}
/*双引号进行转移,这个是自然的啦*/ /*
* 单引号会使后面的占位符失效,是所有该单引号后面的的占位符,直到遇到下一个单引号或者结尾才会终止这种行为
* 有点类似声明单引号内容只是普通字符串的意思
* 使用两个单引号和一个双引号都不会有问题,大概它是将’结束了吧,"被看做两个'
*/
@Test
public void fun6(){
System.out.println(MessageFormat.format("{0}{1}", 1,2));
System.out.println(MessageFormat.format("'{0}{1}", 1,2));
System.out.println(MessageFormat.format("'{0}'{1}", 1,2));
System.out.println(MessageFormat.format("{0}'{1}", 1,2));
} /*
* 只有满足格式化的格式才会执行我们想要的操作,}不会报错,相当于没有影响,{会导致转换异常需要使用单引号转义
*/
@Test
public void fun7(){
System.out.println(MessageFormat.format("{0}{1}}dada", 1,2));
//System.out.println(MessageFormat.format("{0}{1}{dada", 1,2));//非法参数异常
System.out.println(MessageFormat.format("{0}{1}'}'dada", 1,2));
} /*
* 一个有趣的现象,出现多个左花括号(指的是不再格式中使用的,且没有转义的),会分隔字符串(截取0~第一个{的字符),而不是报异常参数非法
*/
@Test
public void fun8(){
System.out.println(MessageFormat.format("{0}{asa{sas{1}'}'dada", 1,2));
System.out.println(MessageFormat.format("{0}{{{{1}'}'dada", 1,2));
} /*
* 关于MessageFormat.format方法
源码:
public static String format(String pattern, Object ... arguments)
{
MessageFormat temp = new MessageFormat(pattern);
return temp.format(arguments);
}
所以如果多次使用同一个模板,建议创建一个MessageFormat实力再执行
*/
@Test
public void fun9(){
MessageFormat temp=new MessageFormat("{0},我喜欢你!--{1}");
String to="mm";
String from="gg";
Object[] testArgs={to,from};
System.out.println(temp.format(testArgs));
System.out.println(temp.format(new String[]{"guodaxia","郭祯"}));
}
/*
* 可以指定格式器进行转换
*/
@Test
public void fun10(){
MessageFormat temp=new MessageFormat("{0,number,long}---{1,number,#.#}");
System.out.println(temp.format(new Number[]{2321.23,232.12}));
} }

aaa

MessFormat的简单使用的更多相关文章

  1. 【造轮子】打造一个简单的万能Excel读写工具

    大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...

  2. Fabio 安装和简单使用

    Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...

  3. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  4. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

  5. 在Openfire上弄一个简单的推送系统

    推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...

  6. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

  7. 使用 Nodejs 搭建简单的Web服务器

    使用Nodejs搭建Web服务器是学习Node.js比较全面的入门教程,因为要完成一个简单的Web服务器,你需要学习Nodejs中几个比较重要的模块,比如:http协议模块.文件系统.url解析模块. ...

  8. ASP.NET Aries 入门开发教程2:配置出一个简单的列表页面

    前言: 朋友们都期待我稳定地工作,但创业公司若要躺下,也非意念可控. 若人生注定了风雨飘摇,那就雨中前行了. 最机开始看聊新的工作机会,欢迎推荐,创业公司也可! 同时,趁着自由时间,抓紧把这系列教程给 ...

  9. 简单入门canvas - 通过刮奖效果来学习

    一 .前言 一直在做PC端的前端开发,从互联网到行业软件.最近发现移动端已经成为前端必备技能了,真是不能停止学习.HTML5新增的一些东西,canvas是用的比较多也比较复杂的一个,简单的入门了一下, ...

随机推荐

  1. 使用ffmpeg下载m3u8流媒体

    安装 编译好的windows可用版本的下载地址(官网中可以连接到这个网站,和官方网站保持同步): http://ffmpeg.zeranoe.com/builds/ 或者: 百度网盘https://p ...

  2. Linux2_小技巧

    0 鼠标不灵么: 左侧设置图标----显示----未知显示屏--关闭 1 左侧自动隐藏 右键---更改桌面背景---行为--隐藏 2 终端打开 搜索到终端添加到左侧 ctrl+alt+T快捷打开 ct ...

  3. Cobbler部署之FAQ处理

    Cobbler报错处理 通过cobbler check检查出现的报错 红色标注为报错关键信息 9.1 报错一 # cobbler check httpd does not appear to be r ...

  4. 实战c++中的vector系列--creating vector of local structure、vector of structs initialization

    之前一直没有使用过vector<struct>,如今就写一个简短的代码: #include <vector> #include <iostream> int mai ...

  5. Android ImageButton的使用。

    1.首先是范例代码,一个基本的ImageButton响应. package com.example.arlxsdemo; import android.graphics.Bitmap; import ...

  6. SDOI 2016 Round1 Day1

    储能表 /* 引自zyz大佬的数学思想 */ #include<cstdio> #include<iostream> using namespace std; typedef ...

  7. 【BZOJ1132】[POI2008]Tro 几何

    [BZOJ1132][POI2008]Tro Description 平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和 N<=3000 Input 第一行给出数字N,N在[3,3000 ...

  8. Java之线程池(一)

    在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统 ...

  9. python3里面的图片处理库 pillow

    在python2下用pil,而在python3下可以安装pillow 功能,在图片上加上几个字 #coding: utf-8 myPath = "./" fontPath = &q ...

  10. C#练习委托、事件、事件处理

    控制台应用程序效果: 代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...