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类的构造方法来创建字符串变量. 声明字符串: ...
随机推荐
- 禁用U盘的两种方法
方法一:注册表 计算机\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet003\Services\USBSTOR 更改值为4即可,恢复时同理 ,重启即可 方法二:组策略
- CRegKey 注册表操作 转
转自 http://blog.csdn.net/pbymw8iwm/article/details/6931946 1.简介 CRegKey提供了对系统注册表的操作方法,通过CRegKey类,可以方便 ...
- 史上最简单的SpringCloud教程 | 第十三篇: 断路器聚合监控(Hystrix Turbine)(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f13-turbine/ 本文出自方志朋的博客 上一篇文章讲述 ...
- Nlog日志出坑合集
.net core框架下nlog不记录: 1.安装NLog.Web.AspNetCore 2.在Startup.cs文件的方法public void Configure(IApplicationBui ...
- Core Data实例
#import <UIKit/UIKit.h> #import <CoreData/CoreData.h> @interface CHViewController : UIVi ...
- NodeJs仿阿帕奇实现浏览某一路径文件目录效果
网页效果 这里实现的效果是将我的电脑下的某一路径文件展现在网页中 html网页代码: <h1>仿阿帕奇网页 </h1> <table> <tr> < ...
- Yii2之发送电子邮件
官方文档:http://www.yiiframework.com/doc-2.0/guide-tutorial-mailing.html 使用Yii2框架的时候,有时候需要发送电子邮件,Yiii2提供 ...
- 月薪30-50K的大数据工程师们,他们背后是如何学习的
这两天小编去了解了下大数据开发相关职位的薪资,主要有hadoop工程师,数据挖掘工程师.大数据算法工程师等,从平均薪资来看,目前大数据相关岗位的月薪均在2万以上,随着项目经验的增长工资会越来越高. ...
- hive常见的几种优化手段
Hive调优的几个入手点: Hive是基于Hadoop框架的,Hadoop框架又是运行在JVM中的,而JVM最终是要运行在操作系统之上的,所以,Hive的调优可以通过如下几个方面入手: 操作系统调优 ...
- 基于Python的飞机大战游戏
前几天决定学Python,上网找了教程看了两天,和C比起来面向对象的特性真的都很便捷,有了类开发各种敌机,子弹什么的都很方便. 在此要感谢开发pygame模块的开发人员,真的很好用(逃 效果图↓ 主函 ...