查找并替换字符串 Find And Replace in String
2018-07-29 17:08:15
问题描述:


问题求解:
字符串替换的问题有个技巧就是从右向左进行替换,这样的话,左边的index就不需要考虑变动了。
public String findReplaceString(String S, int[] indexes, String[] sources, String[] targets) {
List<int[]> ls = new ArrayList<>();
for (int i = 0; i < indexes.length; i++) ls.add(new int[]{indexes[i], i});
Collections.sort(ls, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o2[0] - o1[0];
}
});
for (int[] pair : ls) {
int index = pair[0];
int i = pair[1];
String source = sources[i];
String target = targets[i];
if (S.substring(index, index + source.length()).equals(source))
S = S.substring(0, index) + target + S.substring(index + source.length());
}
return S;
}
查找并替换字符串 Find And Replace in String的更多相关文章
- 在LoadRunner中查找和替换字符串
参考<Search & Replace function for LoadRunner>: http://ptfrontline.wordpress.com/2009/03/13/ ...
- Word 查找和替换字符串方法
因为项目需要通过word模板替换字符串 ,来让用户下载word, 就在网上找了找word查找替换字符串的库或方法,基本上不是收费,就是无实现,或者方法局限性太大 .docx 是通过xml来存储文字和其 ...
- python 替换字符串的方法replace()、正则re.sub()
一.replace()函数1用字符串本身的replace方法: a = 'hello word' b = a.replace('word','python') print b 1 2 3 二.re ...
- Shell:sed用法 - 查找并替换字符串
原文链接 语法 sed 's/serach_str/replace_str/g' file_path 在某个文件中查找所有的serach_str并替换为replace_str 参数 描述 serach ...
- JS查找和替换字符串列子
依赖 工具函数库 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- [Swift]LeetCode890. 查找和替换模式 | Find and Replace Pattern
You have a list of words and a pattern, and you want to know which words in words matches the patter ...
- 关于在vim中的查找和替换
1,查找 在normal模式下按下/即可进入查找模式,输入要查找的字符串并按下回车. Vim会跳转到第一个匹配.按下n查找下一个,按下N查找上一个. Vim查找支持正则表达式,例如/vim$匹配行尾的 ...
- 在 Vim 中优雅地查找和替换(转)
总有人问我 Vim 中能不能查找,当然能!而且是超级强的查找! 这篇文章来详细介绍 Vim 中查找相关的设置和使用方法. 包括查找与替换.查找光标所在词.高亮前景/背景色.切换高亮状态.大小写敏感查找 ...
- 在 Vim 中优雅地查找和替换
原文更好看链接http://harttle.com/2016/08/08/vim-search-in-file.html 总有人问我 Vim 中能不能查找,当然能!而且是超级强的查找! 这篇文章来详细 ...
随机推荐
- KM算法模板
大白书P248有证明,此处贴出两种复杂度的方案, n^4 大白书P350 n^3 #include <algorithm> #include <string.h> #inclu ...
- linux环境下安装jdk1.6
卸载rpm版的jdk: #rpm -qa|grep jdk 显示:jdk1.6.0_29-fcs 卸载:#rpm -e --nodeps jdk1.6.0_29-fcs 1.从sun公司网站www.s ...
- 02: DOM 实例
1.1 Event 对象 <body> <a id="myAnchor" href="http://www.microsoft.com"> ...
- 实验二Java面向对象程序设计
一.单元测试 了解三种代码: 1.伪代码:类似于自然语言说明,描述实现逻辑思维 2.产品代码:程序员编辑的开发代码,要求可修改.可移植 3.测试代码:我理解是相当于开发软件在软件开放之前,程序员找到b ...
- 20145204《网络对抗》MAL后门原理与实践
20145204<网络对抗>MAL后门原理与实践 实践内容说明 (1)使用netcat获取主机操作Shell,cron启动 (1分) (2)使用socat获取主机操作Shell, 任务计划 ...
- Linxu内核版本号后面多出字符串或者+号【学习笔记】
作者:庄泽彬 之前一直没有留意到但是最近在编译内核的时候版本号竟然多出了个加号+号或字符串, 后面终于找到原因了,原来config如果设置了CONFIG_LOCALVERSION_AUTO=y,内核的 ...
- 51nod 1137 矩阵乘法
基本的矩阵乘法 中间for(int j=0;i<n;i++) //这里写错了 应该是j<n 晚上果然 效率不行 等会早点儿睡 //矩阵乘法 就是 两个矩阵 第一个矩阵的列 等与 第 ...
- 谈谈java中的final关键字
知识点:final(最终的)关键字修饰类.方法.属性 1.final修饰类:那么这个就无法被继承,如String类.StringBuffer类.System类 2.final修饰方法:被修饰的方法不能 ...
- [JavaScript] - 7kyu
Johnny is a boy who likes to open and close lockers. He loves it so much that one day, when school w ...
- Apache Kylin1.5.2.1之订单案例详细构建流程
转:http://blog.itpub.net/30089851/viewspace-2122586/ 一.Hive订单数据仓库构建1. 创建事实表并插入数据 DROP TABLE IF EXISTS ...