【问题标题】:Java: How to fill placeholders in a text with Map<String,String>?Java:如何使用 Map<String,String> 在文本中填充占位符?
【发布时间】:2018-03-03 20:01:38
【问题描述】:

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

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;
}

如何将“%s”中的两个替换为“文件名”和“20mb”?

【问题讨论】:

    标签: java string hashmap mapping placeholder


    【解决方案1】:

    您对String#format 的处理方法是错误的。

    它需要可变数量的对象来替换占位符作为第二个参数,而不是地图。要将它们组合在一起,您可以使用数组或列表。

    String format = "Media file %s of size %s has been approved";
    
    Object[] args = {"file name", "20mb"};
    String newNotification = String.format(format, args);
    

    【讨论】:

      【解决方案2】:

      这不是地图的本意。 您添加的是一个条目"file name" -&gt; "20 mb",这基本上意味着属性“文件名”的值是“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()));
      }
      

      【讨论】:

        【解决方案3】:

        我认为%s是Python的语法占位符,不能在Java环境中使用;而你的方法createNotification()定义需要两个参数,不能只给一个。

        【讨论】:

        【解决方案4】:

        您可以简单地使用 var-args 进行格式化:

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

        createNotification方法中传递var-args,代码如下:

        public static String createNotification(String myStr, String... strings){
            String message = ""; 
            message=String.format(myStr, strings[0], strings[1]);
        
            return message;
        }
        

        【讨论】:

        • 但是在每种情况下都有不同数量的字符串被替换。所以我觉得这个不能用。
        • @Sandu_prass 这是给定字符串“大小为 %s 的媒体文件 %s 已被批准”,我们可以通过进行一些更改使其动态化。
        【解决方案5】:

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

        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()
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-11-22
          • 2018-05-05
          • 2018-01-16
          • 2015-11-17
          • 1970-01-01
          • 2018-08-02
          • 1970-01-01
          相关资源
          最近更新 更多