码迷,mamicode.com
首页 > 其他好文 > 详细

replace string use another

时间:2017-12-02 11:19:12      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:ring   查找替换   use   not   div   system   int   out   mic   

  • 第二题: 相当于实现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);

  

replace string use another

标签:ring   查找替换   use   not   div   system   int   out   mic   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!