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. C++基础--引用的一点补充

    这一篇是对引用的一点补充,内容基本上是来自<C++ primer plus>一书第八章的内容. 前面一篇介绍了引用的一点特点,这里补充一个,将引用用于类对象的时候,有一个体现继承的特征,就 ...

  2. StringUtils.format用法

    String.format()字符串常规类型格式化的两种重载方式 format(String format, Object… args) 新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字 ...

  3. Kmp--P3375 【模板】KMP字符串匹配

    题目描述 如题,给出两个字符串 s1 和 s2,其中 s2 为 s1 的子串,求出 s2 在 s1​ 中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组 next. (如果你不知道这 ...

  4. spring boot 接口返回值封装

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  5. iOS基础——通过案例学知识之xib、plist、mvc

    透过案例学习xib的使用.plist的使用.mvc在iOS的使用,今天要做的案例效果图 1.xib和nib xib文件可以被XCode编译成nib文件,xib文件本质上是一个xml文件,而nib文件就 ...

  6. [GWCTF 2019]枯燥的抽奖

    0x00 知识点 种子爆破 工具 http://www.openwall.com/php_mt_seed 0x01 解题 查看源码进入check.php zOHF0Cxp49 <?php #这不 ...

  7. Delphi7 流操作_压缩

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  8. POJ 1088 DP=记忆化搜索

    话说DP=记忆化搜索这句话真不是虚的. 面对这道题目,题意很简单,但是DP的时候,方向分为四个,这个时候用递推就好难写了,你很难得到当前状态的前一个真实状态,这个时候记忆化搜索就派上用场啦! 通过对四 ...

  9. SQL基础教程(第2版)第5章 复杂查询:5-2 子查询

    第5章 复杂查询:5-2 子查询 ● 一言以蔽之,子查询就是一次性视图( SELECT语句).与视图不同,子查询在SELECT语句执行完毕之后就会消失.● 由于子查询需要命名,因此需要根据处理内容来指 ...

  10. RaspBerry--解决无法用 ssh 直接以 root 用户登录

    参考:https://www.cnblogs.com/xwdreamer/p/6604593.html 以普通用户登录,然后切换至 root 用户. 编辑 /etc/ssh/sshd_config 添 ...