Given an original string input, and two strings S and T, replace all occurrences of S in input with T.

Assumptions

  • input, S and T are not null, S is not empty string

Examples

  • input = "appledogapple", S = "apple", T = "cat", input becomes "catdogcat"
  • input = "laicode", S = "code", T = "offer", input becomes "laioffer"
public class Solution {
public String replace(String input, String source, String target) {
// Write your solution here
StringBuilder sb = new StringBuilder();
int start = 0;
int match = input.indexOf(source, start);
while (match != -1) {
// append end index exclusive
sb.append(input, start, match).append(target);
start = match + source.length();
match = input.indexOf(source, start);
}
sb.append(input, start, input.length());
return sb.toString();
}
}

最新文章

  1. 转:CentOS/Debian/Ubuntu一键安装LAMP(Apache/MySQL/PHP)环境
  2. ctf汇总
  3. iOS地图
  4. 命令与文件的查询 which whereis locate find
  5. Python 5 —— OOP
  6. CSS 高级:尺寸、分类、伪类、伪元素
  7. PrintWriter的print和write方法(转)
  8. Js遍历Josn对象(内容对比页实现思路)
  9. PHP文件目录copy
  10. matlab三维画图
  11. spring mvc后台接收中文乱码
  12. Web前端学习(1):上网的过程与网页的本质
  13. Spring MVC Restful Put方法无法获取参数值
  14. [codeforces 804F. Fake bullions]
  15. struts2-剩余
  16. django介绍及路由系统
  17. View的getMeasuredWidth和getWidth有什么区别?
  18. HDU4185 Oil Skimming 二分图匹配 匈牙利算法
  19. Hibernate(1)基本知识
  20. 【java多线程】队列系统之ArrayBlockingQueue源码

热门文章

  1. Java工程师面试题
  2. cf 507E. Breaking Good
  3. springmvc的InternalResourceViewResolver自我理解
  4. hdu 1799 循环多少次?(组合)
  5. mysql第四篇:数据操作之多表查询
  6. CSS(2)之重新认识 CSS3 新特性
  7. nodejs(6)express学习
  8. JavaScript 之 web API
  9. 【MySQL 组复制】1.组复制技术简介
  10. 2019~2020icpc亚洲区域赛徐州站H. Yuuki and a problem