我正在使用一個代碼,我想用另一個字符串填充幾個字符串佔位符。這是我用來測試我的代碼的示例文本。Java:如何用Map <String,String>填充文本中的佔位符?
String myStr = "Media file %s of size %s has been approved"
這是我如何填補佔位符。因爲我期望使用幾個地方持有人我已經使用java地圖<>。
Map<String, String> propMap = new HashMap<String,String>();
propMap.put("file name","20mb");
String newNotification = createNotification(propMap);
我用下面的方法創建字符串。
public String createNotification(Map<String, String> properties){
String message = "";
message = String.format(myStr, properties);
return message;
}
如何用「文件名」和「20mb」替換兩個'%s'?
%S _can_在Java中使用:https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax –