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();
}
}

[Algo] 649. String Replace (basic)的更多相关文章

  1. Java String.replace()方法

    Java String.replace()方法用法实例教程, 返回一个新的字符串,用newChar替换此字符串中出现的所有oldChar 声明 以下是java.lang.String.replace( ...

  2. .net 基础错误-string.replace 方法

    1.string string.Replace(string oldValue,string newValue) 返回一个新的字符串,其中当前示例中出现的所有指定字符串都替换另一个指定字符串 错误:总 ...

  3. Python string replace 方法

    Python string replace   方法 方法1: >>> a='...fuck...the....world............' >>> b=a ...

  4. String.replace与String.format

    字符串的替换函数replace平常使用的频率非常高,format函数通常用来填补占位符.下面简单总结一下这两个函数的用法. 一.String.replace的两种用法 replace的用法如:repl ...

  5. [转]String.Replace 和 String.ReplaceAll 的区别

    JAVA 中的 replace replaceAll 问题: 测试code System.out.println("1234567890abcdef -----> "+&qu ...

  6. JAVA中string.replace()和string.replaceAll()的区别及用法

    乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),replaceall替换所有的字符,其实大不然,只是替换的用途不一样.    public String r ...

  7. python 字符串替换功能 string.replace()可以用正则表达式,更优雅

    说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的. 之前,我的代码写法如下,粗笨: 自从发现了正则表达式也生效后,代码变 ...

  8. [Javascript] How to use JavaScript's String.replace

    In JavaScript, you can change the content of a string using the replace method. This method signatur ...

  9. string::replace

    #include <string> #include <cctype> #include <algorithm> #include <iostream> ...

随机推荐

  1. Kubernetes-基于helm安装部署高可用的Redis及其形态探索(二)

    上一章,我们通过实践和其他文章的帮助,在k8s的环境安装了redis-ha,并且对其进行了一些实验来验证他的主从切换是否有效.本篇中将会分析,究竟是如何实现了redis-ha的主从切换,以及其与K8S ...

  2. 【每日Scrum】第九天冲刺

    一.计划会议内容 尝试数据库的连接与ui应用 二.任务看板 任务看板 已完成:登录与个人界面布局实现 进行中:连接数据库,地图主界面 待进行:功能整合 三.scrum讨论照片 四.产品的状态 无 五. ...

  3. MySQL-复制技术演进过程

    复制技术的演进可以分为:基于数据安全的复制,基于效率的复制 基于数据安全的复制 异步复制 参考: https://baijiahao.baidu.com/s?id=163939455634386120 ...

  4. 吴裕雄--天生自然C++语言学习笔记:C++ 常量

    常量是固定值,在程序执行期间不会改变.这些固定的值,又叫做字面量. 常量可以是任何的基本数据类型,可分为整型数字.浮点数字.字符.字符串和布尔值. 常量就像是常规的变量,只不过常量的值在定义后不能进行 ...

  5. VS常用高效快捷键

    快捷键的使用能够提供我们写代码的效率.还能装逼(哈哈O(∩_∩)O~) 类别 快捷键 描述 编辑 Ctrl+S 保存(养成好习惯,停下来的时候就保存下,不然遇见突发情况会很崩溃的) Ctrl+Shif ...

  6. OO第三单元“技术”博客

    主要针对第三单元的三次作业 JML语言的理论基础.应用工具链情况 JML指的是Java建模语言,全称是Java modeling language,是一种行为接口规范语言,可用于指定Java模块的行为 ...

  7. jenkins+saltstack+pipeline 部署springcloud 多模块jar包

    在jenkins上安装salt-master, pipeline{ agent{       node{               label 'master'               cust ...

  8. Q7:Reverse Integer

    7. Reverse Integer 官方的链接:7. Reverse Integer Description : Given a 32-bit signed integer, reverse dig ...

  9. win10查看显卡算力

    1.查看笔记本自带算力 运行C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\extras\demo_suite目录下的deviceQue ...

  10. ZooKeeper解决的问题

    1.解决分布式单点问题 https://www.jianshu.com/p/08b76bd7a634 2.实现分布式环境数据的一致性.访问ZooKeeper树结构时,不同节点返回的数据是一致,不会引起 ...