📜  替换字符串占位符java代码示例

📅  最后修改于: 2022-03-11 14:52:32.013000             🧑  作者: Mango

代码示例1
public static String dictFormat(String format, Hashtable values) {
    StringBuilder convFormat = new StringBuilder(format);
    Enumeration keys = values.keys();
    ArrayList valueList = new ArrayList();
    int currentPos = 1;
    while (keys.hasMoreElements()) {
        String key = keys.nextElement(),
        formatKey = "%(" + key + ")",
        formatPos = "%" + Integer.toString(currentPos) + "$";
        int index = -1;
        while ((index = convFormat.indexOf(formatKey, index)) != -1) {
            convFormat.replace(index, index + formatKey.length(), formatPos);
            index += formatPos.length();
        }
        valueList.add(values.get(key));
        ++currentPos;
    }
    return String.format(convFormat.toString(), valueList.toArray());
}