[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> ...
随机推荐
- python保留小数位
前言 保留小数位是我们经常会碰到的问题,尤其是刷题过程中.那么在python中保留小数位的方法也非常多,但是笔者的原则就是什么简单用什么,因此这里介绍几种比较简单实用的保留小数位的方法: 方法一:fo ...
- 51Nod 1067 Bash博弈V2
这道题告诉我,一定要去尝试,去推算,不要动不动就找度娘要答案.(惭愧惭愧) 既然是博弈问题,按理我们应该找出规律,怎么找呢,推,把前几项写出来找规律,动手很重要. 上题: 1067 Bash游戏 V2 ...
- DevOps专题|玩转Kubernetes网络
Kubernetes无疑是当前最火热的容器编排工具,网络是kubernetes中非常重要的一环, 本文主要介绍一些相应的网络原理及术语,以及kubernetes中的网络方案和对比. Kubernete ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:比较 和 逻辑运算符
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- go简单文件服务器
go文件服务器 go语言实现的简单文件服务器 github
- Vue 指令 v-text v-html
有三个指令达到的效果是一样的 {{JS表达式}} 差值表达式 v-text="JS表达式" v-html="JS表达式" //会自动解析tag js表达式 ...
- CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)
题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...
- 18 12 23 html 基本学习
一个html的基本结构如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- Maven:Failure executing javac, but could not parse the error:javac: 无效的目标发行版: 1.8
eclipse中对着项目maven——>>maven install时出现错误:Failure executing javac, but could not parse the error ...
- mysql中各种复杂的增删改查
1.mysql查出数据表中连续出现三次或三次以上的数据 建一张表如下:表名为 number 1.1 要求找出num列连续出现三次或三次以上的数据: select * from number where ...