Named placeholders in string formatting

后端 未结 20 1940
情话喂你
情话喂你 2020-11-27 10:16

In Python, when formatting string, I can fill placeholders by name rather than by position, like that:

print \"There\'s an incorrect value \'%(value)s\' in c         


        
相关标签:
20条回答
  • 2020-11-27 10:47

    Another example of Apache Common StringSubstitutor for simple named placeholder.

    String template = "Welcome to {theWorld}. My name is {myName}.";
    
    Map<String, String> values = new HashMap<>();
    values.put("theWorld", "Stackoverflow");
    values.put("myName", "Thanos");
    
    String message = StringSubstitutor.replace(template, values, "{", "}");
    
    System.out.println(message);
    
    // Welcome to Stackoverflow. My name is Thanos.
    
    0 讨论(0)
  • 2020-11-27 10:47

    You can use StringTemplate library, it offers what you want and much more.

    import org.antlr.stringtemplate.*;
    
    final StringTemplate hello = new StringTemplate("Hello, $name$");
    hello.setAttribute("name", "World");
    System.out.println(hello.toString());
    
    0 讨论(0)
  • 2020-11-27 10:47

    For very simple cases you can simply use a hardcoded String replace, no need for a library there:

        String url = "There's an incorrect value '%(value)' in column # %(column)";
        url = url.replace("%(value)", x); // 1
        url = url.replace("%(column)", y); // 2
    

    WARNING: I just wanted to show the simplest code possible. Of course DO NOT use this for serious production code where security matters, as stated in the comments: escaping, error handling and security are an issue here. But in the worst case you now know why using a 'good' lib is required :-)

    0 讨论(0)
  • 2020-11-27 10:48

    Apache Commons Lang's replaceEach method may come in handy dependeding on your specific needs. You can easily use it to replace placeholders by name with this single method call:

    StringUtils.replaceEach("There's an incorrect value '%(value)' in column # %(column)",
                new String[] { "%(value)", "%(column)" }, new String[] { x, y });
    

    Given some input text, this will replace all occurrences of the placeholders in the first string array with the corresponding values in the second one.

    0 讨论(0)
  • 2020-11-27 10:49

    Try Freemarker, templating library.

    0 讨论(0)
  • 2020-11-27 10:52

    I tried in just a quick way

    public static void main(String[] args) 
    {
        String rowString = "replace the value ${var1} with ${var2}";
        
        Map<String,String> mappedValues = new HashMap<>();
        
        mappedValues.put("var1", "Value 1");
        mappedValues.put("var2", "Value 2");
        
        System.out.println(replaceOccurence(rowString, mappedValues));
    }
    
    private static  String replaceOccurence(String baseStr ,Map<String,String> mappedValues)
    {
        for(String key :mappedValues.keySet())
        {
            baseStr = baseStr.replace("${"+key+"}", mappedValues.get(key));
        }
        
        return baseStr;
    }
    
    0 讨论(0)
提交回复
热议问题