replace string use another

  • 第二题: 相当于实现Microsoft里面的查找替换的功能,给一个string article, 一个string find,一个string replace,把文章里所有的find都替换成replace,例如abcdbc把bc换成e ---->aede。思路比较简单,直接indexOf做了,只不过本来应该是用indexOf(“bc”,index),我给写成indexOf(index,“bc”)了(这个是错的)。写完小哥让我口头跑test case,发现一个bug修复,然后剩下5分钟了。
String s = "abcdabc";
        String tt = "bc";
        String ttt = "e";
        int index = 0;
        while (s.indexOf("bc", index) != -1) {
            index = s.indexOf("bc", index);
            s = s.substring(0, index) + ttt + s.substring(index + tt.length());
        }
        System.out.println(s);

  

原文地址:https://www.cnblogs.com/apanda009/p/7952874.html