Named placeholders in string formatting

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

    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 :-)

提交回复
热议问题