在Python中,格式化字符串时,我可以按名称而不是按位置填充占位符,如下所示:

print "There's an incorrect value '%(value)s' in column # %(column)d" % \
  { 'value': x, 'column': y }

我想知道这在Java中是否可能(希望没有外部库)?


当前回答

你可以使用StringTemplate库,它提供了你想要的和更多。

import org.antlr.stringtemplate.*;

final StringTemplate hello = new StringTemplate("Hello, $name$");
hello.setAttribute("name", "World");
System.out.println(hello.toString());

其他回答

另一个Apache Common StringSubstitutor的简单命名占位符的例子。

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.

我的答案是:

a)尽可能使用StringBuilder

b)保持“占位符”的位置(以任何形式:整数是最好的,特殊字符如dollar宏等),然后使用StringBuilder.insert()(参数的几个版本)。

当StringBuilder内部转换为String时,使用外部库似乎有些过度,而且我认为会显著降低性能。

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!"

我试了一下

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

这是一个旧的线程,但只是为了记录,你也可以使用Java 8风格,像这样:

public static String replaceParams(Map<String, String> hashMap, String template) {
    return hashMap.entrySet().stream().reduce(template, (s, e) -> s.replace("%(" + e.getKey() + ")", e.getValue()),
            (s, s2) -> s);
}

用法:

public static void main(String[] args) {
    final HashMap<String, String> hashMap = new HashMap<String, String>() {
        {
            put("foo", "foo1");
            put("bar", "bar1");
            put("car", "BMW");
            put("truck", "MAN");
        }
    };
    String res = replaceParams(hashMap, "This is '%(foo)' and '%(foo)', but also '%(bar)' '%(bar)' indeed.");
    System.out.println(res);
    System.out.println(replaceParams(hashMap, "This is '%(car)' and '%(foo)', but also '%(bar)' '%(bar)' indeed."));
    System.out.println(replaceParams(hashMap, "This is '%(car)' and '%(truck)', but also '%(foo)' '%(bar)' + '%(truck)' indeed."));
}

输出将是:

This is 'foo1' and 'foo1', but also 'bar1' 'bar1' indeed.
This is 'BMW' and 'foo1', but also 'bar1' 'bar1' indeed.
This is 'BMW' and 'MAN', but also 'foo1' 'bar1' + 'MAN' indeed.