MessageFormatUtils { public static String format(final String template, final Object... args) { if (template == null) { return ""; } if (args == null || args.length == 0) { return template; } 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(); }  static String format(final String template, final Object... args);  }
@Test public void format() throws Exception { assertEquals("", MessageFormatUtils.format(null)); assertEquals("just a message", MessageFormatUtils.format("just a message")); assertEquals("just a message", MessageFormatUtils.format("{} a message", "just")); assertEquals("just a message", MessageFormatUtils.format("just a {}", "message")); assertEquals("just a message", MessageFormatUtils.format("{} a {}", "just", "message")); assertEquals("This is just a message.", MessageFormatUtils.format("This is {} a {}.", "just", "message")); assertEquals("This is another text.", MessageFormatUtils.format("This is {} {}.", "another", "text")); assertEquals("Result: {1} is 2x2=4.", MessageFormatUtils.format("Result: {1} is {}={}.", "2x2", "4")); }