String replacement method of stencil MessageFormat.format (String pattern, Object ... arguments)

MessageFormat.format(String pattern, Object ... arguments)

MessageFormat.format(NewOrderConstant.PAY_SCHEMA,
        URLEncoder.encode(MessageFormat.format(NewOrderConstant.RESULT_SCHEME, String.valueOf(orderId)), HTTP.UTF_8),
        URLEncoder.encode(MessageFormat.format(NewOrderConstant.RESULT_SCHEME, String.valueOf(orderId)), HTTP.UTF_8),
        URIUtil.encodeQuery(shopBO.getShopName(), HTTP.UTF_8),
        this.toStringWithRemoveTrailingZeros(huiAmount),
        NewOrderConstant.BIZ_ORDER_TYPE_OEDER,
        String.valueOf(orderId),
        String.valueOf(shopId),
        noHuiAmount == null ? "0" : this.toStringWithRemoveTrailingZeros(noHuiAmount),
        (shopBO.getType() == ShopTypeCode.IntellRestaurant_PRE_PAY || shopBO.getType() == ShopTypeCode.HBT_PRE_PAY) ? 1 : 0,
        URLEncoder.encode("{supportVoucher:" + shopBO.getHuiType() + "}", HTTP.UTF_8));

The following is the code to achieve:

In fact, this realization is the use of the MessageFormat class;

String content = "ab, cc, {name}, {password}, ​​{DATE}, dd, ff";

String array[] = {userName, password, format.format(new Date())};
content = MessageFormat.format(content, array);

Explained as follows:

content needs to be replaced is in {} parameter, array stored in the array is to be replaced corresponding to the parameter; MessageFormat method used when the need for these parameters will be the number of correct matches, and to specify the sequence number, otherwise, matching error. This realization of replaceable parameters. Very simple, and very rigid.

MessageFormat: From java.text package;

We can open your own jdk source code to read his implementation.

This switched https://blog.csdn.net/qq_20906499/article/details/51436732

Guess you like

Origin www.cnblogs.com/snake107/p/12047295.html