MessFormat的简单使用

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

原文地址:https://www.cnblogs.com/aigeileshei/p/5767111.html