Named placeholders in string formatting

后端 未结 20 1944
情话喂你
情话喂你 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 11:04

    There is Java Plugin to use string interpolation in Java (like in Kotlin, JavaScript). Supports Java 8, 9, 10, 11…​ https://github.com/antkorwin/better-strings

    Using variables in string literals:

    int a = 3;
    int b = 4;
    System.out.println("${a} + ${b} = ${a+b}");
    

    Using expressions:

    int a = 3;
    int b = 4;
    System.out.println("pow = ${a * a}");
    System.out.println("flag = ${a > b ? true : false}");
    

    Using functions:

    @Test
    void functionCall() {
        System.out.println("fact(5) = ${factorial(5)}");
    }
    
    long factorial(int n) {
        long fact = 1;
        for (int i = 2; i <= n; i++) {
            fact = fact * i;
        }
        return fact;
    }
    

    For more info, please read the project README.

    0 讨论(0)
  • 2020-11-27 11:05

    You could have something like this on a string helper class

    /**
     * An interpreter for strings with named placeholders.
     *
     * For example given the string "hello %(myName)" and the map <code>
     *      <p>Map<String, Object> map = new HashMap<String, Object>();</p>
     *      <p>map.put("myName", "world");</p>
     * </code>
     *
     * the call {@code format("hello %(myName)", map)} returns "hello world"
     *
     * It replaces every occurrence of a named placeholder with its given value
     * in the map. If there is a named place holder which is not found in the
     * map then the string will retain that placeholder. Likewise, if there is
     * an entry in the map that does not have its respective placeholder, it is
     * ignored.
     *
     * @param str
     *            string to format
     * @param values
     *            to replace
     * @return formatted string
     */
    public static String format(String str, Map<String, Object> values) {
    
        StringBuilder builder = new StringBuilder(str);
    
        for (Entry<String, Object> entry : values.entrySet()) {
    
            int start;
            String pattern = "%(" + entry.getKey() + ")";
            String value = entry.getValue().toString();
    
            // Replace every occurence of %(key) with value
            while ((start = builder.indexOf(pattern)) != -1) {
                builder.replace(start, start + pattern.length(), value);
            }
        }
    
        return builder.toString();
    }
    
    0 讨论(0)
  • 2020-11-27 11:06

    I created also a util/helper class (using jdk 8) which can format a string an replaces occurrences of variables.

    For this purpose I used the Matchers "appendReplacement" method which does all the substitution and loops only over the affected parts of a format string.

    The helper class isn't currently well javadoc documented. I will changes this in the future ;) Anyway I commented the most important lines (I hope).

        public class FormatHelper {
    
        //Prefix and suffix for the enclosing variable name in the format string.
        //Replace the default values with any you need.
        public static final String DEFAULT_PREFIX = "${";
        public static final String DEFAULT_SUFFIX = "}";
    
        //Define dynamic function what happens if a key is not found.
        //Replace the defualt exception with any "unchecked" exception type you need or any other behavior.
        public static final BiFunction<String, String, String> DEFAULT_NO_KEY_FUNCTION =
                (fullMatch, variableName) -> {
                    throw new RuntimeException(String.format("Key: %s for variable %s not found.",
                                                             variableName,
                                                             fullMatch));
                };
        private final Pattern variablePattern;
        private final Map<String, String> values;
        private final BiFunction<String, String, String> noKeyFunction;
        private final String prefix;
        private final String suffix;
    
        public FormatHelper(Map<String, String> values) {
            this(DEFAULT_NO_KEY_FUNCTION, values);
        }
    
        public FormatHelper(
                BiFunction<String, String, String> noKeyFunction, Map<String, String> values) {
            this(DEFAULT_PREFIX, DEFAULT_SUFFIX, noKeyFunction, values);
        }
    
        public FormatHelper(String prefix, String suffix, Map<String, String> values) {
            this(prefix, suffix, DEFAULT_NO_KEY_FUNCTION, values);
        }
    
        public FormatHelper(
                String prefix,
                String suffix,
                BiFunction<String, String, String> noKeyFunction,
                Map<String, String> values) {
            this.prefix = prefix;
            this.suffix = suffix;
            this.values = values;
            this.noKeyFunction = noKeyFunction;
    
            //Create the Pattern and quote the prefix and suffix so that the regex don't interpret special chars.
            //The variable name is a "\w+" in an extra capture group.
            variablePattern = Pattern.compile(Pattern.quote(prefix) + "(\\w+)" + Pattern.quote(suffix));
        }
    
        public static String format(CharSequence format, Map<String, String> values) {
            return new FormatHelper(values).format(format);
        }
    
        public static String format(
                CharSequence format,
                BiFunction<String, String, String> noKeyFunction,
                Map<String, String> values) {
            return new FormatHelper(noKeyFunction, values).format(format);
        }
    
        public static String format(
                String prefix, String suffix, CharSequence format, Map<String, String> values) {
            return new FormatHelper(prefix, suffix, values).format(format);
        }
    
        public static String format(
                String prefix,
                String suffix,
                BiFunction<String, String, String> noKeyFunction,
                CharSequence format,
                Map<String, String> values) {
            return new FormatHelper(prefix, suffix, noKeyFunction, values).format(format);
        }
    
        public String format(CharSequence format) {
    
            //Create matcher based on the init pattern for variable names.
            Matcher matcher = variablePattern.matcher(format);
    
            //This buffer will hold all parts of the formatted finished string.
            StringBuffer formatBuffer = new StringBuffer();
    
            //loop while the matcher finds another variable (prefix -> name <- suffix) match
            while (matcher.find()) {
    
                //The root capture group with the full match e.g ${variableName}
                String fullMatch = matcher.group();
    
                //The capture group for the variable name resulting from "(\w+)" e.g. variableName
                String variableName = matcher.group(1);
    
                //Get the value in our Map so the Key is the used variable name in our "format" string. The associated value will replace the variable.
                //If key is missing (absent) call the noKeyFunction with parameters "fullMatch" and "variableName" else return the value.
                String value = values.computeIfAbsent(variableName, key -> noKeyFunction.apply(fullMatch, key));
    
                //Escape the Map value because the "appendReplacement" method interprets the $ and \ as special chars.
                String escapedValue = Matcher.quoteReplacement(value);
    
                //The "appendReplacement" method replaces the current "full" match (e.g. ${variableName}) with the value from the "values" Map.
                //The replaced part of the "format" string is appended to the StringBuffer "formatBuffer".
                matcher.appendReplacement(formatBuffer, escapedValue);
            }
    
            //The "appendTail" method appends the last part of the "format" String which has no regex match.
            //That means if e.g. our "format" string has no matches the whole untouched "format" string is appended to the StringBuffer "formatBuffer".
            //Further more the method return the buffer.
            return matcher.appendTail(formatBuffer)
                          .toString();
        }
    
        public String getPrefix() {
            return prefix;
        }
    
        public String getSuffix() {
            return suffix;
        }
    
        public Map<String, String> getValues() {
            return values;
        }
    }
    

    You can create a class instance for a specific Map with values (or suffix prefix or noKeyFunction) like:

        Map<String, String> values = new HashMap<>();
        values.put("firstName", "Peter");
        values.put("lastName", "Parker");
    
    
        FormatHelper formatHelper = new FormatHelper(values);
        formatHelper.format("${firstName} ${lastName} is Spiderman!");
        // Result: "Peter Parker is Spiderman!"
        // Next format:
        formatHelper.format("Does ${firstName} ${lastName} works as photographer?");
        //Result: "Does Peter Parker works as photographer?"
    

    Further more you can define what happens if a key in the values Map is missing (works in both ways e.g. wrong variable name in format string or missing key in Map). The default behavior is an thrown unchecked exception (unchecked because I use the default jdk8 Function which cant handle checked exceptions) like:

        Map<String, String> map = new HashMap<>();
        map.put("firstName", "Peter");
        map.put("lastName", "Parker");
    
    
        FormatHelper formatHelper = new FormatHelper(map);
        formatHelper.format("${missingName} ${lastName} is Spiderman!");
        //Result: RuntimeException: Key: missingName for variable ${missingName} not found.
    

    You can define a custom behavior in the constructor call like:

    Map<String, String> values = new HashMap<>();
    values.put("firstName", "Peter");
    values.put("lastName", "Parker");
    
    
    FormatHelper formatHelper = new FormatHelper(fullMatch, variableName) -> variableName.equals("missingName") ? "John": "SOMETHING_WRONG", values);
    formatHelper.format("${missingName} ${lastName} is Spiderman!");
    // Result: "John Parker is Spiderman!"
    

    or delegate it back to the default no key behavior:

    ...
        FormatHelper formatHelper = new FormatHelper((fullMatch, variableName) ->   variableName.equals("missingName") ? "John" :
                FormatHelper.DEFAULT_NO_KEY_FUNCTION.apply(fullMatch,
                                                           variableName), map);
    ...
    

    For better handling there are also static method representations like:

    Map<String, String> values = new HashMap<>();
    values.put("firstName", "Peter");
    values.put("lastName", "Parker");
    
    FormatHelper.format("${firstName} ${lastName} is Spiderman!", map);
    // Result: "Peter Parker is Spiderman!"
    
    0 讨论(0)
  • 2020-11-27 11:08
    public static String format(String format, Map<String, Object> values) {
        StringBuilder formatter = new StringBuilder(format);
        List<Object> valueList = new ArrayList<Object>();
    
        Matcher matcher = Pattern.compile("\\$\\{(\\w+)}").matcher(format);
    
        while (matcher.find()) {
            String key = matcher.group(1);
    
            String formatKey = String.format("${%s}", key);
            int index = formatter.indexOf(formatKey);
    
            if (index != -1) {
                formatter.replace(index, index + formatKey.length(), "%s");
                valueList.add(values.get(key));
            }
        }
    
        return String.format(formatter.toString(), valueList.toArray());
    }
    

    Example:

    String format = "My name is ${1}. ${0} ${1}.";
    
    Map<String, Object> values = new HashMap<String, Object>();
    values.put("0", "James");
    values.put("1", "Bond");
    
    System.out.println(format(format, values)); // My name is Bond. James Bond.
    
    0 讨论(0)
  • 2020-11-27 11:09

    StrSubstitutor of jakarta commons lang is a light weight way of doing this provided your values are already formatted correctly.

    http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/text/StrSubstitutor.html

    Map<String, String> values = new HashMap<String, String>();
    values.put("value", x);
    values.put("column", y);
    StrSubstitutor sub = new StrSubstitutor(values, "%(", ")");
    String result = sub.replace("There's an incorrect value '%(value)' in column # %(column)");
    

    The above results in:

    "There's an incorrect value '1' in column # 2"

    When using Maven you can add this dependency to your pom.xml:

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-27 11:11

    not quite, but you can use MessageFormat to reference one value multiple times:

    MessageFormat.format("There's an incorrect value \"{0}\" in column # {1}", x, y);
    

    The above can be done with String.format() as well, but I find messageFormat syntax cleaner if you need to build complex expressions, plus you dont need to care about the type of the object you are putting into the string

    0 讨论(0)
提交回复
热议问题