增强字符串格式化功能,支持索引和顺序占位符

This commit is contained in:
Xujiayao 2026-01-20 16:53:20 +08:00
parent 1394aba988
commit 403877ac9a

View file

@ -22,16 +22,50 @@ public class StringUtils {
}
/**
* Simple {} placeholder replacement.
* Formats a string with placeholders.
* <p>
* Supports both sequential "{}" and indexed "{0}", "{1}" placeholders.
* Note: Do not mix both styles in the same string.
*
* @param str String with {} placeholders
* @param str String with placeholders
* @param args Arguments to replace the placeholders
* @return String with placeholders replaced
*/
public static String format(String str, Object... args) {
for (Object arg : args) {
str = str.replaceFirst("\\{}", arg == null ? "null" : arg.toString().replace("\\", "\\\\"));
if (str == null || args == null || args.length == 0) {
return str;
}
return str;
// Check if the string uses indexed placeholders (e.g., "{0}", "{1}")
// We assume that if it contains "{0}", it's using indexed mode.
if (str.contains("{0}")) {
for (int i = 0; i < args.length; i++) {
String target = "{" + i + "}";
String replacement = args[i] == null ? "null" : args[i].toString();
str = str.replace(target, replacement);
}
return str;
}
// Otherwise, use sequential "{}" replacement
StringBuilder sb = new StringBuilder(str.length());
int searchStart = 0;
int argIndex = 0;
while (argIndex < args.length) {
int placeholderIndex = str.indexOf("{}", searchStart);
if (placeholderIndex == -1) {
break;
}
sb.append(str, searchStart, placeholderIndex);
sb.append(args[argIndex] == null ? "null" : args[argIndex].toString());
searchStart = placeholderIndex + 2;
argIndex++;
}
sb.append(str.substring(searchStart));
return sb.toString();
}
}