[Algo] 649. String Replace (basic)
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)的更多相关文章
- Java String.replace()方法
Java String.replace()方法用法实例教程, 返回一个新的字符串,用newChar替换此字符串中出现的所有oldChar 声明 以下是java.lang.String.replace( ...
- .net 基础错误-string.replace 方法
1.string string.Replace(string oldValue,string newValue) 返回一个新的字符串,其中当前示例中出现的所有指定字符串都替换另一个指定字符串 错误:总 ...
- Python string replace 方法
Python string replace 方法 方法1: >>> a='...fuck...the....world............' >>> b=a ...
- String.replace与String.format
字符串的替换函数replace平常使用的频率非常高,format函数通常用来填补占位符.下面简单总结一下这两个函数的用法. 一.String.replace的两种用法 replace的用法如:repl ...
- [转]String.Replace 和 String.ReplaceAll 的区别
JAVA 中的 replace replaceAll 问题: 测试code System.out.println("1234567890abcdef -----> "+&qu ...
- JAVA中string.replace()和string.replaceAll()的区别及用法
乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),replaceall替换所有的字符,其实大不然,只是替换的用途不一样. public String r ...
- python 字符串替换功能 string.replace()可以用正则表达式,更优雅
说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的. 之前,我的代码写法如下,粗笨: 自从发现了正则表达式也生效后,代码变 ...
- [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 ...
- string::replace
#include <string> #include <cctype> #include <algorithm> #include <iostream> ...
随机推荐
- 人人嫌丑的iPhoneX刘海屏为何会被手机厂商竞相模仿
不得不提到的是,苹果的iPhone自发布以来就始终引领着智能手机发展的方向.比如iPhone一代出现之后,就慢慢将键盘手机赶下历史舞台,让触屏手机成为主流.此外,iPhone的指纹识别.金属机身.玻璃 ...
- sql拆分列 时间拆分 日、月、年
我想 查看今日访问人数 需要分组查询 就得 时间拆分 这两天百度 方法有很多 substring ... 但是 我这一列 是时间类型: 可以直接用 DATEPART() 函数用于返回日期/时间的单独部 ...
- schema 文件约束
1. 在javaproject 中创建一个.xsd 文件 <?xml version="1.0" encoding="UTF-8" ?> <! ...
- 吴裕雄--天生自然TensorFlow2教程:数据加载
import tensorflow as tf from tensorflow import keras # train: 60k | test: 10k (x, y), (x_test, y_tes ...
- A. Yellow Cards ( Codeforces Round #585 (Div. 2) 思维水题
---恢复内容开始--- output standard output The final match of the Berland Football Cup has been held recent ...
- Linux(CENTOS7) NodeJs安装
1.下载NodeJs 官网下载地址:http://nodejs.cn/download/ 2.上传到linux系统 我这里上传到/disk/nodejs目录下面的,上传工具使用的xftp. 3 ...
- BZOJ 3197 [Sdoi2013]assassin
题解: 树上Hash 首先重心在边上就把边分裂 以重心为根建树,这样两个根一定对应 然后f[i][j]表示i匹配另一棵的j节点的最小代价 把他们的儿子摘出来做最小权匹配即可 #include<i ...
- jQuery 1.3.2 简单实现select二级联动
jQuery 1.3.2 简单实现select二级联动 复制代码代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...
- OA|DOAJ|Highwire press|Springeropen|Plos journal|电子印本|中国科技论文在线|arxiv|chinaxiv|MIT机构知识库|中科院机构知识库|Email alert|Citeseer|RSS|F1000 prime
信息检索 OA:open access开放获取 金色OA:出版社主导, 开放出版,全部都可以下载. 开放论文:只有部分可以下载. 绿色OA:作者主导,发表后放在机构知识库中,排版不同,但是内容一致.E ...
- AXURE方便的功能
(1)建立一个公共的页面,可以把一些常用的组建放进去,就和代码要封装方法一样,这样省区了用到一次画一次的麻烦. 其中可以包括:弹出框.图标.搜索框之类的. 当然还可以把经常用到的 登陆.注册.忘记密码 ...