MessageFormat:格式化字符串

需求

有一个字符串:"[张三]负责的位置[东厂房]发生[烟雾]报警,请[张三]及时前往处理。"。实际需要对[]内的内容进行替换。如果调用String.replace(),那需要执行多次。而使用MessageFormat.format,则可以替换一次即可获取目标字符串:

String[] args = { "张三", "东厂房", "烟雾" };
String info = MessageFormat.format("[{0}]负责的位置[{1}]发生[{2}]报警,请[{0}]及时前往处理。", args);

MessageFormat.format()

MessageFormat.format()由jdk提供,可对字符串进行格式化。
MessageFormat.format(String pattern, Object ... arguments)的参数:

  • pattern:模板字符串。其中需要使用{ArgumentIndex }的形式标记要替换的内容(FormatElement)。ArgumentIndex从0开始,表示替换为arguments的ArgumentIndex索引元素。
  • arguments:替换参数,是个Object数组。

例如:

String info = MessageFormat.format("{0}是{1}", "天", "蓝色"); // 天是蓝色

其中{0}即为FormatElement,有3种格式:

  • { ArgumentIndex }
  • { ArgumentIndex, FormatType }
  • { ArgumentIndex, FormatType, FormatStyle }

在3种格式中,有3个参数:

  • ArgumentIndex :arguments的索引。

  • FormatType:FormatElement的格式化类型。若要按String进行格式化格式化,不传入该参数。但若需要按其他数据类型格式化,有4种取值:

    • number:数值。使用NumberFormat处理。
    • date:日期。使用DateFormat处理。
    • time:时间。使用DateFormat处理。
    • choice:键值对。使用ChoiceFormat处理。
  • FormatStyle:格式化类型的具体样式,用于对FormatType进行细化。取值为:

    • short
    • medium
    • long
    • full
    • integer
    • currency
    • percent
    • SubformatPattern:子格式。需指定格式串。

例如:

// number
MessageFormat.format("{0, number, #.##}", 1.2345f); // 1.23
MessageFormat.format("{0, number, currency}", 1.23); // ¥1.2
MessageFormat.format("{0, number, percent}", 1.23); // 123%

Date date = new Date();

// date
MessageFormat.format("{0, date}", date); // 2023-11-15

MessageFormat.format("{0, date, short}", date); // 23-11-15
MessageFormat.format("{0, date, medium}", date); // 2023-11-15
MessageFormat.format("{0, date, long}", date); // 2023年11月15日
MessageFormat.format("{0, date, full}", date); // 2023年11月15日 星期三

MessageFormat.format("{0, date, yyyy-MM-dd}", date); // 2023-11-15
MessageFormat.format("{0, date, yyyy-MM-dd HH:mm:ss}", date); // 2023-11-15 15:00:00

// time
MessageFormat.format("{0, time}", date); // 15:00:00
MessageFormat.format("{0, time}", date.getTime()); // 15:00:00
MessageFormat.format("{0, time, short}", date); // 下午3:00
MessageFormat.format("{0, time, medium}", date); // 15:00:00
MessageFormat.format("{0, time, long}", date); // 下午03时00分00秒
MessageFormat.format("{0, time, full}", date); // 下午03时00分00秒 CST
MessageFormat.format("{0, time, HH:mm}", date); // 15:00

关于choice则需定义size相同的两个数组:double和String数组来进行映射。可参考ChoiceFormat的用法。

特殊字符"{"

MessageFormat有2个特殊字符:{'
'用于标记特殊字符,使用2个'将特殊字符包裹起来即可。注意如果只有一个'则会被忽略。
MessageFormat默认是将{n}作为占位符,其解析时先识别{,因此{是个特殊字符。如果有非占位符的{,需要使用2个'将其包裹起来。}不是特殊字符,如果没有{仅有},则会正常输出。

最佳实践

MessageFormat.format()的源码为:

public static String format(String pattern, Object ... arguments) {
    MessageFormat temp = new MessageFormat(pattern);
    return temp.format(arguments);
}

可见其创建了一个新的MessageFormat对象。
如果需要对相同格式的字符串多次格式化,那么建议暂存一个MessageFormat对象来多次使用。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值