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. JSP标签 <fmt:formatDate>格式化日期

    <fmt:formatDate>标签用于使用不同的方式格式化日期. <%@ page language="java" contentType="text ...

  2. cf 261B.Maxim and Restaurant

    什么什么期望的,不会! (题解http://blog.sina.com.cn/s/blog_140e100580102wj4e.html(看不懂)) #include<bits/stdc++.h ...

  3. LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)

    题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as ...

  4. LIS是什么?【标本分拣】

    接之前[LIS是什么?]中,提到几点需要补充描述的部分. Ⅰ.标本分管处理 标本的分管处理,在医院和第三方实验室有多种叫法,例如:分拣.合管等等.这里我称之为分拣,分拣实际上分为两个部分:系统中标本分 ...

  5. (6)Mat对象的一些函数和方法的使用

    首先是基本的代码整理 #include<iostream> #include<opencv.hpp> using namespace std; using namespace ...

  6. 2020/1/30 PHP代码审计之CSRF漏洞

    0x00 CSRF漏洞 CSRF(Cross-site request forgery)跨站请求伪造:也被称为"One Click Attack"或者Session Riding, ...

  7. maven项目集成Quartz定时任务框架,实现批处理功能

    一.Quartz简介 主要做定时任务,即:在指定时间点或时间段,执行某项任务,可设置执行次数.时间间隔等. 二.Springcloud简介 对比传统的.庞大的.复杂的.以ssm或ssh为框架的web项 ...

  8. 关于Java的String字符串常量的长度问题

    虽然这种问题应该很难遇到,但是遇到了也会感到莫名其妙.不知道大家有没有遇到那种在java代码里用字符串写sql语句的情况,但是如果sql语句字符串的长度太长的话就会报错. 代码如下: 代码A Stri ...

  9. {转} MJPG流媒体在HTML5的呈现方案

    最近碰到的需求:监控探头视频呈现到html页面上. 视频源协议:HLS; 视频源格式:Motion JPEG 简称 MJPG; 其中Motion JPEG(M-JPEG或MJPEG,Motion Jo ...

  10. 教你如何使用JavaScript入门

    JavaScript简介   JavaScript是NetScape公司为Navigator浏览器开发的,是web前端卸载HTML文件中的一种脚本语言,能实现网页内容的交互显示.当用户在客户端显示该网 ...