String

 public static String formatMessage(String message, String...params) {

        if (params == null || params.length == 0) {
            return message;
        }
        StringBuffer buf = new StringBuffer();
        int leftPos = 0;
        int lastPos = 0;
        while ((leftPos = message.indexOf(STR_LEFTKAKKO, lastPos)) >= 0) {
            int rightPos = message.indexOf("}", leftPos);
            if (rightPos < 0) {
                break;
            } else {
                int nexLeftPos = message.indexOf(STR_LEFTKAKKO, leftPos + 2);
                if (rightPos > nexLeftPos && nexLeftPos > 0) {
                    buf.append(message.substring(lastPos, nexLeftPos));
                    lastPos = nexLeftPos;
                    continue;
                }
            }
            String sIdx = message.substring(leftPos + 2, rightPos);
            int nIdx = 0;
            try {
                nIdx = Integer.parseInt(sIdx);
            } catch (NumberFormatException e) {
                nIdx = Integer.MAX_VALUE;
            }
            if (nIdx > params.length) {
                buf.append(message.substring(lastPos, rightPos + 1));
                lastPos = rightPos + 1;
            } else {
                buf.append(message.substring(lastPos, leftPos));
                buf.append(params[nIdx - 1]);
                lastPos = rightPos + 1;
            }
        }
        buf.append(message.substring(lastPos));
        return buf.toString();
    }

你可能感兴趣的:(String)