Java字符串格式化,{}占位符根据名字替换实例我就废话不多说了,⼤家还是直接看代码吧~
import java.beans.PropertyDescriptor;
import flect.Method;
import java.util.HashMap;
import java.util.Map;
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<String, Object> 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, Class());
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<String, Object> 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<String,Object> 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]的⽅式插⼊参数,只是参数个数应⽤不灵活,还是使⽤数组的⽅式取值⽐较好,
public static void format(){
String st="%s的%s的价格是%f,是否售罄%c,占总销售的%d%%,库存%d,是否为畅销品%b";
String s=String.format(st, "58优品","啤酒",3.5,'是',50,199,true);
System.out.println(s);
}
public static void main(String[] args) {
//传⼊参数为指定表名,和参数值
//StringFormat.formSql("user","role", "1","3","5","7","9","33");
format();
}
public static void format(){
//String st="%s的%s的价格是%f,是否售罄%c,占总销售的%d%%,库存%d,是否为畅销品%b";
//String s=String.format(st, "58优品","啤酒",3.5,'是',50,199,true);
//System.out.println(s);
SimpleDateFormat simple=new SimpleDateFormat("yyyy年MM⽉dd⽇ HH点mm分ss秒");
String newDate=simple.format(new Date());
String st1="%s的%s的价格是%s,是否售罄%s,占总销售的%s%%,库存%s,是否为畅销品%s,当前⽇期为%s";    String ss=String.format(st1, "58优品","啤酒","3.5",'是',"80","998","true",newDate);
System.out.println(ss);
}
public static void main(String[] args) {
//传⼊参数为指定表名,和参数值
//StringFormat.formSql("user","role", "1","3","5","7","9","33");
format();
}
/*%S字符串类型的占位符
* %c字符类型的占位符
* %b布尔类型的占位符
* %d整数类型的占位符
* %%百分⽐类型的占位符
* %n换⾏类型的占位符
* %t时间类型的占位符
* c全部的⽇期时间类型
* F年-⽉-⽇格式
* D年/⽉/⽇格式
* rHH:MM:SS格式12⼩时制
*
*/
以上这篇Java字符串格式化,{}占位符根据名字替换实例就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

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