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类的构造方法来创建字符串变量. 声明字符串: ...
随机推荐
- springboot——我的第一个工程
前言:使用Spring Boot 微服务架构有一段时间了,打算从今天开始记录使用过程. 一.Spring Boot介绍: 简介:Spring Boot 框架的产生,是为了方便我们简化Spring 框架 ...
- 可决系数R^2和MSE,MAE,SMSE
波士顿房价预测 首先这个问题非常好其实要完整的回答这个问题很有难度,我也没有找到一个完整叙述这个东西的资料,所以下面主要是结合我自己的理解和一些资料谈一下r^2,mean square error 和 ...
- HDU 1273 漫步森林(数学 找规律)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1273 漫步森林 Time Limit: 2000/1000 MS (Java/Others) M ...
- Faster Alternatives to glReadPixels and glTexImage2D in OpenGL ES
In the development of Shou, I’ve been using GLSL with NEON to manipulate image rotation, scaling and ...
- 嵌入式 Linux 学习 之路
1. 嵌入式 Linux (首先百度了一下) 结果没有 看到 有信息的内容.2017年2月17日10:06:51 (嵌入式Linux 英文名:embedded Linux 简称 eLinux,Git ...
- 使用nuget 打包并上传 nuget.org
一. 准备工作 1 下载 Download NuGet.exe 2 windows 系统下设置环境变量 path中 或者 在dos 命令窗口下转到 nuget.exe 所在目录 3 在www.nu ...
- Mahout介绍
3.11简介 Mahout:是一个Apache的一个开源的机器学习库,主要实现了三大类算法Recommender (collaborative filtering).Clustering.classi ...
- jquery固定位置浮动
示例: <!DOCTYPE html> <html> <head> <title>test page</title> <script ...
- 用模板引擎Art-Template渲染空格或换行符引发的一场“命案”
一.绪论 说实话,真的不知道如何给这篇博客命名,因为我觉得应该有一些小伙伴遇到跟我同样的问题正在抓耳挠腮中. 二.导火索 最近在做一个移动H5翻页的功能,类似于MAKA模板那种.假设大致框架如下 ...
- [HTML]在页面中输出空格的几种方式
JS中如何输出空格 在写JS代码的时候,大家可以会发现这样现象: document.write(" 1 2 3 "); 结果: 1 2 ...