查找并替换字符串 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 中能不能查找,当然能!而且是超级强的查找! 这篇文章来详细 ...
随机推荐
- 【调优】Nginx性能调优
一.Nginx优化配置 1.主配置文件优化:# vi /usr/local/nginx/conf/nginx.conf----------------------------------------- ...
- suiyi
<?php namespace app\controllers; use Yii;use app\models\Device;use app\models\DeviceSearch;use ap ...
- AutoLayout 的一些坑
1. 给一个 UIView 加约束,希望它显示在 UITableView 的底部,但是它不显示,它会出现在 UITableView 的顶部. 错误代码: [self.tableView addSubv ...
- Java Callable接口——有返回值的线程
实际开发过程中,我们常常需要等待一批线程都返回结果后,才能继续执行.<线程等待——CountDownLatch使用>中我们介绍了CountDownLatch的使用,通过使用CountDow ...
- python 简单的爬虫
import urllib.request import re import ssl # 处理https请求 import time import os # 创建目录用 def get_html(ur ...
- MATLAB 简明教程
MATAB 是我学习和接触的第一种工具类的编程语言,最早可以追溯到大一上数学分析这门课的时候.MATLAB既是一种软件也是一门编程语言,MATLAB功能强大在理科和工科中运用较多. MATLAB 是 ...
- Android :64位支持的说明
https://blog.csdn.net/u012400885/article/details/52923765 https://blog.csdn.net/ouyang_peng/article/ ...
- 汽车变智能只靠ADAS?麦克风也是主角
在先进驾驶辅助系统(ADAS)中,结合视觉处理器的CMOS影像感测器已在协助汽车辨识与分类方面发挥关键作用.至于其“听觉”呢? 麦克风也能扮演像摄影机般重要的角色,为自动驾驶车增添更多“智慧”功能吗? ...
- JavaScript 方法扩展
一.String全部替换方法 String.prototype.replaceAll = function(s1, s2){ return this.replace(new RegExp(s1, &q ...
- Core Java 2
p267~p270: 1.一个方法不仅需要告诉编译器将要返回什么值, 还要告诉编译器有可能发生什么错误(以便在错误发生时用妥善的方式处理错误). 2.方法应该在首部声明所有可能抛出的异常. 3.方法抛 ...