class MessageFormatUtils {

    public static String format(final String template, final Object... args) {

        if (template == null) {
            return "";
        }

        if (args == null || args.length == 0) {
            return template; // nothing to replace with
        }

        int index = 0;
        int next = template.indexOf(PLACEHOLDER);
        if (next == -1) {
            return template;
        }

        String remainder = template;
        final StringBuilder out = new StringBuilder();
        while (next != -1) {
            out.append(remainder.substring(0, next));
            final Object arg = nextArg(args, index++);
            out.append(arg);
            remainder = remainder.substring(next + PLACEHOLDER.length());
            next = remainder.indexOf(PLACEHOLDER);
        }

        if (remainder.length() > 0) {
            out.append(remainder);
        }

        return out.toString();

    }

}