代码之家  ›  专栏  ›  技术社区  ›  sndu

Java:如何用Map<String,String>填充文本中的占位符?

  •  3
  • sndu  · 技术社区  · 8 年前

    我正在使用一个代码,我想用另一个字符串填充几个字符串占位符。这是我用来测试代码的示例文本。

    String myStr = "Media file %s of size %s has been approved"
    

    这就是我如何填充占位符的方法。因为我希望使用几个占位符,所以我使用了java Map<>。

    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”替换'%1!'中的两个?

    5 回复  |  直到 8 年前
        1
  •  3
  •   daniu    8 年前

    这不是地图的意图。 "file name" -> "20 mb"

    List .

    因此,你想要的是

    public String createNotification(String[] properties) {
        assert(properties.length == 2); // you might want to really check this, you will run into problems if it's false
        return String.format("file %s has size %s", properties);
    }
    

    Map<String,String> yourMap = //...
    for (Entry<String,String> e : yourMap) {
        System.out.println(createNotification(e.getKey(), e.getValue()));
    }
    
        2
  •  3
  •   Luciano van der Veekens    8 年前

    您的方法 String#format 是错误的。

    它希望有数量可变的对象替换占位符作为第二个参数,而不是映射。要将它们全部分组,可以使用数组或列表。

    String format = "Media file %s of size %s has been approved";
    
    Object[] args = {"file name", "20mb"};
    String newNotification = String.format(format, args);
    
        3
  •  1
  •   Mohit Tyagi    8 年前

        String myStr = "Media file %s of size %s has been approved";
    
        String newNotification = createNotification(myStr, "file name", "20mb");
    
        System.out.println(newNotification);
    

    传入变量参数 createNotification

    public static String createNotification(String myStr, String... strings){
        String message = ""; 
        message=String.format(myStr, strings[0], strings[1]);
    
        return message;
    }
    
        4
  •  0
  •   Mohit Tyagi    8 年前

    我想 %s 是Pythons语法的占位符,不能在Java环境中使用;还有你的方法 createNotification()

        5
  •  0
  •   sndu    8 年前

    在尝试了几种方法后,终于找到了一个很好的解决方案。占位符必须如下[占位符]。

    public String createNotification(){
        Pattern pattern = Pattern.compile("\\[(.+?)\\]");
        Matcher matcher = pattern.matcher(textTemplate);
        HashMap<String,String> replacementValues = new HashMap<String,String>();
        StringBuilder builder = new StringBuilder();
        int i = 0;
        while (matcher.find()) {
            String replacement = replacementValues.get(matcher.group(1));
            builder.append(textTemplate.substring(i, matcher.start()));
            if (replacement == null){ builder.append(matcher.group(0)); }      
            else { builder.append(replacement); }     
            i = matcher.end();
        }
        builder.append(textTemplate.substring(i, textTemplate.length()));
        return builder.toString()
    }