String替换占位符
/**
* 依次替换占位符
* 例如: 姓名:{s},电话:{s},邮箱:{s} --> 姓名:小张,电话:18800000001,邮箱:abc@123.com
* pattern = "\\{s}";
*
* @param input
* @param pattern
* @param texts
* @param nullStr 不能为null
* @return
*/
public static String appendReplacement(String input, String pattern, String[] texts, String nullStr) {
// 从正则表达式实例中运行方法并查看其如何运行
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
StringBuffer sb = new StringBuffer();
int i = 0;
while (m.find()) {
// 将匹配之前的字符串复制到sb,再将匹配结果替换掉,并追加到sb
m.appendReplacement(sb, i < texts.length && texts[i] != null ? texts[i] : nullStr);
i++;
}
m.appendTail(sb);
return sb.toString();
}
Implements a non-terminal append-and-replace step. This method performs the following actions: 1. It reads characters from the input sequence, starting at theappend position, and appends them to the given string buffer. Itstops after reading the last character preceding the previous match,that is, the character at index start() - 1. 2. It appends the given replacement string to the string buffer. 3. It sets the append position of this matcher to the index ofthe last character matched, plus one, that is, to end(). The replacement string may contain references to subsequencescaptured during the previous match: Each occurrence of ${name} or $gwill be replaced by the result of evaluating the corresponding group(name) or group(g)respectively. For $g,the first number after the $ is always treated as part ofthe group reference. Subsequent numbers are incorporated into g ifthey would form a legal group reference. Only the numerals '0'through '9' are considered as potential components of the groupreference. If the second group matched the string "foo", forexample, then passing the replacement string "$2bar" wouldcause "foobar" to be appended to the string buffer. A dollarsign ($) may be included as a literal in the replacementstring by preceding it with a backslash (\$). Note that backslashes (\) and dollar signs ($) inthe replacement string may cause the results to be different than if itwere being treated as a literal replacement string. Dollar signs may betreated as references to captured subsequences as described above, andbackslashes are used to escape literal characters in the replacementstring. This method is intended to be used in a loop together with the appendTail and find methods. Thefollowing code, for example, writes one dog two dogs in theyard to the standard-output stream: Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());
String替换占位符的更多相关文章
- 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
一. Application用途 1. Application用途 创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例; Appl ...
- maven 编译替换占位符
首先开启资源配置的插件,由此插件替换占位符 <plugin> <groupId>org.apache.maven.plugins</groupId> <art ...
- Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
一. Application用途 1. Application用途 创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例; Appl ...
- Roslyn 使用 Target 替换占位符方式生成 nuget 打包
本文告诉大家如何编写在编译过程修改打包文件 在项目文件的相同文件夹可以放一个 nuspec 用来告诉 VisualStudio 如何打包 现在尝试创建一个项目 NearjerbetearDeeyito ...
- spring中PropertyPlaceholderHelper替换占位符的值
1.Properties中的值替换¥{}或者#{}占位符 String text = "foo=${foo},bar=${bar}"; Properties props = new ...
- Lua中string.format占位符的使用
虽然lua中字符串拼接"string.format"相对于".."消耗较大,但有时为了代码的可读性,项目中还是经常用到"string.format&q ...
- 安卓程序代写 网上程序代写[原]Android项目中string.xml占位符
开发中经常遇到这样的情况 , 在string.xml中用到以下占位符 <string name="delete_success">删除<xliff:g id=&q ...
- 【占位符替换】替换String中的占位符标志位{placeholder}
概述 占位符替换, 占位符表示为:{placeholder}; 示例:替换如下{xxx}占位符中的内容 "名字:{name},年龄:{age},学校:{school}" 提供了两种 ...
- spring源码解析(一)---占位符解析替换
一.结构类图 ①.PropertyResolver : Environment的顶层接口,主要提供属性检索和解析带占位符的文本.bean.xml配置中的所有占位符例如${}都由它解析 ②.Config ...
随机推荐
- 使用kubeadm进行单master(single master)和高可用(HA)kubernetes集群部署
kubeadm部署k8s 使用kubeadm进行k8s的部署主要分为以下几个步骤: 环境预装: 主要安装docker.kubeadm等相关工具. 集群部署: 集群部署分为single master(单 ...
- Maven仓库介绍以及私服搭建
1 什么是Maven? 1.1 Maven的概念 Maven主要服务于基于Java平台的项目构建.依赖管理和项目信息开发,它是一个异常强大的构建工具,能够帮助我们自动化构建过程,从清理.编译.测试 ...
- urllib基础
import urllib.request # urlretrieve(网址,本地路径) 直接下载网页到本地 urllib.request.urlretrieve("http://www.b ...
- PB事件/函数的触发机制和触发方式
PB作为windows下的一个非常便捷的DB开发工具,有着和windows一样的消息触发机制PB提供了相应event/function触发机制和触发方式,用户可以根据自己的实际需要选用不同方法. 1. ...
- kubernetes kubeadm安装v1.14
1.我们这里准备两台Centos7的主机用于安装,后续节点可以根究需要添加即可:master node01两台都得改:cat /etc/hosts192.168.71.134 master192.16 ...
- 使用 js 简单的实现 bind、call 、aplly
Function.prototype._call = function(obj,...arg){ var me = this; var k = Symbol("test"); // ...
- SQLServer · 最佳实践 · 如何将SQL Server 2012降级到2008 R2-博客-云栖社区-阿里云
迁移须知 使用SQLSERVER 2012的特性在SQL 2008 R2不支持,比如新的分页方式 此迁移操作手册适用于MSSQL2012到MSSQL2008R2的迁移 迁移使用微软提供的脚本生成和导入 ...
- Spark机器学习API之特征处理(一)
Spark机器学习库中包含了两种实现方式,一种是spark.mllib,这种是基础的API,基于RDDs之上构建,另一种是spark.ml,这种是higher-level API,基于DataFram ...
- MySQL下载安装图文
一. MySQL下载 1. 进入MySQL官网官网地址:https://www.mysql.com/ 2. 点击DOWNLOADS 3. 点击Community(GPL) Downloads 4. 找 ...
- 小程序e.currentTarget与e.target 两个属性的区别
注册事件是获取小程序组件上面的自定义属性值 e.target是获取当前点击的标签上面的自定义属性 e.currentTarget是获取注册点击事件标签内的自定义属性