java姓名替换_Java字符串格式化,{}占位符根据名字替换实例我就废话不多说了,⼤家还是直接看代码吧~
import java.beans.propertydescriptor;
import hod;
import java.util.hashmap;
import java.util.map;
java replace方法import matcher;
import pattern;
public class stringformatutil {
private static final pattern pattern = patternpile("\\{(.*?)\\}");
private static matcher matcher;
/**
* 格式化字符串 字符串中使⽤{key}表⽰占位符
*
* @param sourstr
* 需要匹配的字符串
* @param param
* 参数集
* @return
*/
public static string stringformat(string sourstr, map param) {
string tagerstr = sourstr;
if (param == null)
return tagerstr;
try {
matcher = pattern.matcher(tagerstr);
while (matcher.find()) {
string key = up();
string keyclone = key.substring(1, key.length() - 1).trim();
object value = (keyclone);
if (value != null)
tagerstr = place(key, string());
}
}catch (exception e){
return null;
}
return tagerstr;
}
/**
* 格式化字符串 字符串中使⽤{key}表⽰占位符 利⽤反射 ⾃动获取对象属性值 (必须有get⽅法) *
* @param sourstr 需要匹配的字符串
*
* @return
*/
public static string stringformat(string sourstr, object obj) {
string tagerstr = sourstr;
matcher = pattern.matcher(tagerstr);
if (obj == null)
return tagerstr;
propertydescriptor pd;
method getmethod;
// 匹配{}中间的内容 包括括号
while (matcher.find()) {
string key = up();
string keyclone = key.substring(1, key.length() - 1).trim();
try {
pd = new propertydescriptor(keyclone, lass());
getmethod = pd.getreadmethod();// 获得get⽅法
object value = getmethod.invoke(obj);
if (value != null)
tagerstr = place(key, string());
} catch (exception e) {
// todo auto-generated catch block
// loggers.addexception(e);
}
}
return tagerstr;
}
/**
* 格式化字符串 (替换所有) 字符串中使⽤{key}表⽰占位符
*
* @param sourstr
* 需要匹配的字符串
* @param param
* 参数集
* @return
*/
public static string stringformatall(string sourstr, map param) { string tagerstr = sourstr;
if (param == null)
return tagerstr;
try {
matcher = pattern.matcher(tagerstr);
while (matcher.find()) {
string key = up();
string keyclone = key.substring(1, key.length() - 1).trim(); object value = (keyclone);
if (value != null)
tagerstr = place(key, string());
}
}catch (exception e){
return null;
}
return tagerstr;
}
/**
* 格式花字符串,按照占位符名字
* 输⼊:sourstr = xxxxx{a}xxxx{b} ,param = {a:a,b:b}
* 输出:targetstr = xxxxaxxxxb
* @param sourstr
* @param param
* @return
*/
public static string stringformat(string sourstr, jsonobject param) { string tagerstr = sourstr;
if (param == null)
return tagerstr;
try {
matcher = pattern.matcher(tagerstr);
while (matcher.find()) {
string key = up();
string keyclone = key.substring(1, key.length() - 1).trim();
object value = (keyclone);
if (value != null)
tagerstr = place(key, string());
}
}catch (exception e){
return null;
}
return tagerstr;
}
public static void main(string[] args) {
// map map = new hashmap<>();
// map.put("id","111");
// map.put("sss","ss");
/
/ jsonobject json = new jsonobject();
// json.put("id","212");
// json.put("fff","xxxx");
// json.put("emmmmm",11);
// stringformat("sisas&{fff}_diwahwi%{id}{jio}",json);
}
}
补充知识:java中占位符的使⽤
⼆话不说,先上代码
package com.string.format;
public class stringformat {
/
/占位符%s,拼接sql,删除两个表中的数据,条件是字符串数组类型的id
public static void formsql(string tablename,string strings){
//sql占位符 %s占位符
string sql="delete from %s,%s where id in (%s)";
//声明新的字符串
string sqls="";
//遍历字符串的参数数组
for (string str : strings) {
//将参数数组拼接成字符串,⽤逗号分割
sqls += str + ",";
}
/
/拼接最后会多出个逗号,截取
sqls=sqls.substring(0, sqls.length()-1);
//format第⼀个sql参数为⽬标字符串,tablename,tablename2,为替换的两表的名字,sqls为删除数据的参数集合
string s=string.format(sql, tablename,tablename2,sqls);
//输出拼接后的sql
system.out.println(s);
}
public static void main(string[] args) {
//传⼊参数为指定表名,和参数值
stringformat.formsql("user","role", "1","3","5","7","9","33");
}
}
其实,传⼊的参数是数组类型的 值,我们也可以按array[0],array[1]的⽅式插⼊参数,只是参数个数应⽤不灵活,还是使⽤数组的⽅式取值⽐较好,

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。