解决replace遍历循环调用,导致后续的replace替换掉前面replace的数据的问题

我们经常有循环对每个字符串进行replace的操作,如

public static void main(String[] args) {
	String str= "方法1 && 方法2 && 方法3";
	String fun1 = "\"1\".equals(a)";
	String fun2 = "\"2\".equals(b)";
	String fun3 = "\"3\".equals(c)";
	 
	// 进行替换
	str = str.replace("方法1", fun1);
	str = str.replace("方法2", fun2);
	str = str.replace("方法3", fun3);
	System.out.println(str);
 }
输出结果:"1".equals(a) && "2".equals(b) && "3".equals(c)

看起来代码很完美,可是若是换为以下代码的话

public static void main(String[] args) {
        String str = "方法1 && 方法2 && 方法3";
        String fun1 = "\"方法2\".equals(a)";
        String fun2 = "\"方法3\".equals(b)";
        String fun3 = "\"3\".equals(c)";
         
        // 进行替换
        str = str.replace("方法1", fun1);
        str = str.replace("方法2", fun2);
        str = str.replace("方法3", fun3);
        System.out.println(str);
    }

很容易看出来,我们想要的结果应该是如下:

"方法2".equals(a) && “方法3”.equals(b) && “3”.equals©
可是实际上我们得到的结果为:

"""3".equals(c)".equals(b)".equals(a) && ""3".equals(c)".equals(b) && "3".equals(c)

这大大地违背了初衷,可以看到 str = str.replace(“方法1”, fun1);执行完成之后, str就变为了
"方法2".equals(a) && 方法2 && 方法3,继续执行 str = str.replace(“方法2”, fun2);
就会连带将前面的方法2也给替换掉了。 那么如何解决这个问题呢?

我们可以使用下面的代码来提供思路进行解决

public static void main(String[] args) {
	String str = "方法1 && 方法2 && 方法3";
	String fun1 = "\"方法2\".equals(a)";
	String fun2 = "\"方法3\".equals(b)";
	String fun3 = "\"3\".equals(c)";
	
	String str2 = "%1$s && %2$s && %3$s";
	System.out.println(String.format(str2, fun1, fun2, fun3));
}

可以看到,已经输出了正确的结果,那么接下来就需要将其封装为一个方法

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author LaiYongBin
 * @date 创建于 2022/4/15 14:55
 * @apiNote DO SOMETHING
 */
public class Test {
    /**
     * 将str值进行循环替换
     *
     * @param str        要替换的字符串
     * @param replaceMap 替换的Map<String,String>  key:要替换的字符串 value:替换后的字符串
     */
    public static String replaceFormat(String str, Map<String, String> replaceMap) {
        int index = 1;
        Set<String> keySet = replaceMap.keySet();
        String[] valueArray = new String[keySet.size()];
        for (String key : keySet) {
            String value = replaceMap.get(key);
            str = str.replace(key, "%" + index + "$s");
            valueArray[index - 1] = value;
            index++;
        }
        return String.format(str, valueArray);
    }

    public static void main(String[] args) {
        String str = "方法1 && 方法2 && 方法3";
        String fun1 = "\"方法2\".equals(a)";
        String fun2 = "\"方法3\".equals(b)";
        String fun3 = "\"3\".equals(c)";
        Map<String, String> map = new HashMap<>(4);
        map.put("方法1", fun1);
        map.put("方法2", fun2);
        map.put("方法3", fun3);
        String result = replaceFormat(str, map);
        System.out.println(result);
    }

}

大功告成!!!

我不知道使用有现成的JDK自带的API可以实现,若是有,欢迎指出

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原来你是小幸运

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值