java占位符替換五種方式小結(jié)
簡介
在業(yè)務(wù)開發(fā)中,經(jīng)常需要輸出文本串。其中,部分場景下大部分文本內(nèi)容是一樣的,只有少部分點(diǎn)是不一樣。簡單做法是直接拼接字段,但是這會有個(gè)問題。后面如果想要修改內(nèi)容,那么每個(gè)地方都要修改,這不符合設(shè)計(jì)模式中的開閉原則,面向應(yīng)該對擴(kuò)展開放,對修改關(guān)閉。
如何解決這個(gè)問題? 先來看現(xiàn)實(shí)生活中的例子,個(gè)人信息填寫。一般會要填寫表格,已經(jīng)定義好了姓名,性別,年齡等字段,只要填寫對應(yīng)的個(gè)人信息就好了。在程序開發(fā)中,我們會預(yù)先定義好一個(gè)字符串模板,需要改動的點(diǎn)使用占位符。在使用中,使用具體的內(nèi)容替換掉占位符,就能生成我們所需要的文本內(nèi)容。
本文總結(jié)了占位符替換五種方法。前三種方法占位符格式支持自定義。本文使用${param!}的格式,前三種方法示例模板為:
String TEMPLATE = "姓名:${name!},年齡:${age!},手機(jī)號:${phone!}";
待填充參數(shù)為:
private static final Map<String, String> ARGS = new HashMap<String, String>() {{ put("name", "張三"); put("phone", "10086"); put("age", "21"); }};
StringUtils.replace
StringUtils.replace方法定義:
String replace(String text, String searchString, String replacement);
StringUtils.replace能夠?qū)⑷雲(yún)⒅械膕earchString字符串替換成replacement字符串。在使用時(shí),參數(shù)名稱補(bǔ)充上占位符標(biāo)記。
示例:
String remark = TEMPLATE; for (Map.Entry<String, String> entry : ARGS.entrySet()) { remark = StringUtils.replace(remark, "${" + entry.getKey() + "!}", entry.getValue()); } System.out.println(remark);
正則表達(dá)式Matcher
使用正則表達(dá)式依次找到占位符的位置,使用實(shí)際參數(shù)替換掉占位符。
示例:
Pattern TEMPLATE_ARG_PATTERN = Pattern.compile("\\$\\{(.+?)!}"); // ${param!} Matcher m = TEMPLATE_ARG_PATTERN.matcher(TEMPLATE); StringBuffer sb = new StringBuffer(); while (m.find()) { String arg = m.group(1); String replaceStr = ARGS.get(arg); m.appendReplacement(sb, replaceStr != null ? replaceStr : ""); } m.appendTail(sb); System.out.println(sb.toString());
StringSubstitutor
StringSubstitutor功能比較多,使用起來比較靈活。支持默認(rèn)占位符${param}, 當(dāng)然也可以自定義前綴和后綴,甚至支持默認(rèn)值。
public <V> StringSubstitutor(Map<String, V> valueMap); public <V> StringSubstitutor(Map<String, V> valueMap, String prefix, String suffix);
示例:
StringSubstitutor sub = new StringSubstitutor(ARGS,"${","!}"); System.out.println(sub.replace(TEMPLATE));
MessageFormat.format
Java的自帶類MessageFormat支持做占位符替換,占位符格式為{index}, index是索引號。
String template = "Hello, my home is {0} and I am {1} years old."; Object[] replageArgs ={"Alice",25}; String message = MessageFormat.format(template, replageArgs);
String.format
String.format同樣可以實(shí)現(xiàn)占位符替換。
String template = "Hello, my name is %s and I am %d years old."; Object[] replageArgs ={"Alice",25}; String message = String.format(template, replageArgs); System.out.println(message); // 輸出:Hello, my name is Alice and I am 25 years old.
總結(jié)
本文總結(jié)了占位符替換的五種方法。MessageFormat.format和String.format java自帶功能,無需引入外部jar包,能夠完成基本的替換功能。StringUtils.replace, 正則表達(dá)式,StringSubstitutor支持自定義占位符格式,能夠完成更復(fù)雜的功能。
參考:
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/StrSubstitutor.html
https://developer.aliyun.com/article/1257908
到此這篇關(guān)于java占位符替換五種方式小結(jié)的文章就介紹到這了,更多相關(guān)java占位符替換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中yml多環(huán)境配置的3種方法
這篇文章主要給大家介紹了SpringBoot中yml多環(huán)境配置的3種方法,文中有詳細(xì)的代碼示例供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-10-10java 單元測試 對h2數(shù)據(jù)庫數(shù)據(jù)清理方式
這篇文章主要介紹了java 單元測試 對h2數(shù)據(jù)庫數(shù)據(jù)清理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Springboot整合Spring Cloud Kubernetes讀取ConfigMap支持自動刷新配置的教程
這篇文章主要介紹了Springboot整合Spring Cloud Kubernetes讀取ConfigMap支持自動刷新配置,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09