Here you can find the source of format(String str, Object... args)
public static String format(String str, Object... args)
//package com.java2s; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static String format(String str, Object... args) { if (isEmpty(str)) return ""; if (args.length == 0) return str; String result = str;/* www . j ava 2 s . c o m*/ Pattern p = java.util.regex.Pattern.compile("\\{(\\d+)\\}"); Matcher m = p.matcher(str); while (m.find()) { int index = Integer.parseInt(m.group(1)); if (index < args.length && args[index] != null) { result = result.replace(m.group(), args[index].toString()); } } return result; } public static boolean isEmpty(String value) { return value == null || value.trim().equals("") || value.length() == 0; } }