




本文详解如何遍历字符串,对目标字符(如 'l')从左到右逐次计数,并将其原位置替换为当前累计出现序号(如第1次出现→'1',第2次→'2'),支持任意长度计数,避免因数字位数增长导致索引偏移问题。
在实际开发中,常需实现“按首次/二次/第N次出现顺序标记指定字符”的需求,例如将 "helololol" 中的 'l' 替换为 1,2,3,4 得到 "he1o2o3o4",或将 'o' 替换为 "hel1l2l3l"。该任务看似简单,但直接使用 String.substring() 拼接会引发索引错位——因为每次用数字(如 "10" 代替 'l')会使字符串变长,后续字符位置前移,原下标失效。
最稳健的方式是不修改原字符串,而是遍历并累积构建结果,同时动态维护计数器:
public static String replaceCharWithCount(String str, char target) {
StringBuilder result = new StringBuilder();
int count = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == target) {
count++;
result.append(count); // 直接追加当前序号(自动支持多位数)
} else {
result.append(c);
}
}
return result.toString();
}
// 使用示例
public static void main(String[] args) {
System.out.println(replaceCharWithCount("helololol", 'l')); // he1o2o3o4
System.out.println(replaceCharWithCount("helololol", 'o')); // hel1l2l3l
System.out.println(replaceCharWithCount("lllll", 'l')); // 12345
System.out.println(replaceCharWithCount("abcldefglhijkl", 'l')); // abc1defg2hijk3
}✅ 优势:
if (str == null) return null;
替代“边遍历边 substring 拼接”这种易出错的旧范式,应始终优先采用 StringBuil 