Java: Replace a string from multiple replaced strings to multiple substitutes
Provide helper methods to replace a string from multiple replaced strings to multiple substitutes
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringHelper {
/**
* This is test method to replace a string from:
* aaa > zzz
* \t > /t
* \r > /r
* \n > /n
*/
public static void run()
{
String[] replacedStrings = new String[] {"aaa", "\t", "\r", "\n"};
String[] replacements = new String[] {"zzz", "/t", "/r", "/n"};
Pattern pattern = getReplacePattern(replacedStrings);
// The result is "zzza/tb/rc/nd/te/nf"
System.out.println(replace("aaaa\tb\rc\nd\te\nf", pattern, replacedStrings, replacements));
// The result is "zzza/tb/rc/nd/te/nf/r"
System.out.println(replace("aaaa\tb\rc\nd\te\nf\r", pattern, replacedStrings, replacements));
}
/**
* Return a Pattern instance from a specific replaced string array.
* @param replacedStrings replaced strings
* @return a Pattern instance
*/
public static Pattern getReplacePattern(String[] replacedStrings)
{
if (replacedStrings == null || replacedStrings.length == 0) return null;
String regex = "";
for (String replacedString : replacedStrings)
{
if (regex.length() != 0)
{
regex = regex + "|";
}
regex = regex + "(" + replacedString + ")";
}
regex = regex + "";
return Pattern.compile(regex, Pattern.DOTALL | Pattern.MULTILINE);
}
/**
* Replace a string.
* @param value the string.
* @param pattern the Pattern instance.
* @param replacedStrings the replaced string array.
* @param replacements the replacement array.
* @return
*/
public static String replace(String value, Pattern pattern, String[] replacedStrings, String[] replacements)
{
if (pattern == null) return value;
if (replacedStrings == null || replacedStrings.length == 0) return value;
if (replacements == null || replacements.length == 0) return value;
if (replacedStrings.length != replacements.length)
{
throw new RuntimeException("replacedStrings length must same as replacements length.");
}
Matcher matcher = pattern.matcher(value);
StringBuffer buffer = new StringBuffer();
int lastIndex = 0;
while (matcher.find()) {
buffer.append(value.subSequence(lastIndex, matcher.start()));
lastIndex = matcher.end();
String group = matcher.group();
for (int i = 0; i < replacedStrings.length; i++)
{
if (group.equals(replacedStrings[i]))
{
buffer.append(replacements[i]);
break;
}
}
}
buffer.append(value.subSequence(lastIndex, value.length()));
return buffer.toString();
}
}
Java: Replace a string from multiple replaced strings to multiple substitutes的更多相关文章
- Java基础-关键字-String
1.String的本质 线程安全 打开String的源码,类注释中有这么一段话“Strings are constant; their values cannot be changed after t ...
- Java基础知识强化101:Java 中的 String对象真的不可变吗 ?
1. 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对 ...
- 为什么Java中的String是设计成不可变的?(Why String is immutable in java)
There are many reasons due to the string class has been made immutable in Java. These reasons in vie ...
- 深入理解Java中的String
一.String类 想要了解一个类,最好的办法就是看这个类的实现源代码,来看一下String类的源码: public final class String implements java.io.Ser ...
- java中的String设计原理
首先,必须强调一点:String Pool不是在堆区,也不是在栈区,而是存在于方法区(Method Area) 解析: String Pool是常量池(Constant Pool)中的一块. 我们知 ...
- Java中的String为什么是不可变的?
转载:http://blog.csdn.net/zhangjg_blog/article/details/18319521 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那 ...
- Java replace() 方法
Java replace() 方法 Java String类 replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串. 语法 publ ...
- Java学习之String对象为什么是不可变的
转自:http://www.2cto.com/kf/201401/272974.html,感谢作者的总结 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变 ...
- Java之字符串String,StringBuffer,StringBuilder
String类: String类即字符串类型,并不是Java的基本数据类型,但可以像基本数据类型一样使用,用双引号括起来进行声明.在Java中用String类的构造方法来创建字符串变量. 声明字符串: ...
随机推荐
- WIN10下的Docker安装
1.什么是Docker Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱 ...
- 【题解】洛谷P2679 [NOIP2015TG] 子串(DP+滚动数组)
次元传送门:洛谷P2679 思路 蒟蒻一开始并没有思路而去看了题解 我们发现对于两个字串的位置 我们只需要管他们匹配成功或者匹配失败即可 f[i][j][k] 记录当前 a[i]不论等不等于b[j] ...
- HDU4825 Xor Sum
题意 给定一个集合后, 求一组查询中每个数和集合中任一元素异或的最大值. 题解 异或的规律是这样的 1 ^ 1 = 0, 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 而最大值即是 ...
- 微信小程序跳H5页面
主页面:index.wxml 主页面:index.js ↑跳转到另一个wxml页面→recharge.wxml recharge.wxml web-view中设置跳转h5的链接,可以加上需要的参数: ...
- php开发微信图灵机器人
本着开源为原则,为这个世界更美好作出一份共享,我就给大家做个指路人,如果实用,记得给提供开源的朋友一些鼓励. 简单介绍一下实现思路,使用swoole扩展接管php运行,由于swoole只能在类UNIX ...
- 系统优化怎么做-JVM优化之VisualVM
大家好,这里是「聊聊系统优化 」,并在下列地址同步更新 博客园:http://www.cnblogs.com/changsong/ 知乎专栏:https://zhuanlan.zhihu.com/yo ...
- 【oracle笔记3】多表查询
*多表查询 分类:1.合并结果集 2.连接查询 3.子查询 *合并结果集:要求被合并的表中,列的类型和列数相同. *UNION,去除重复行.完全相同的行会被去除 *UNION ALL:不去除重复行. ...
- OC录制小视频
OC录制小视频 用 AVCaptureSession + AVCaptureMovieFileOutput 来录制视频,并通过AVAssetExportSeeion 手段来压缩视频并转换为 MP4 格 ...
- 常用模块 - shutil模块
一.简介 shutil – Utility functions for copying and archiving files and directory trees.(用于复制和存档文件和目录树的实 ...
- 关于linux‘RedHat6.9在VMware虚拟机中的安装步骤
redhat支持多种安装方式:光盘安装,硬盘安装和网络安装等,可以根据个人的实际情况来选择.我在这里选择的是光盘安装的方式安装RHEL6.9.(以下简称6.9) 1.首先准备好6.9的光盘镜像,在安装 ...