Named placeholders in string formatting

后端 未结 20 2271
情话喂你
情话喂你 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

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

提交回复
热议问题