Java: Equivalent to Python's str.format() -


in python, there nice method simplifies creation of strings, making code beautiful , readable.

for example, following code print exampleprogram -e- cannot something

print_msg('e', 'cannot something') def print_msg(type, msg):     print 'exampleprogram -{0}- {1}'.format(type, msg) 

i.e. can specify "slots" within string, using {x} syntax, x parameter index, , returns new string, in replaces these slots parameters passed .format() method.

currently java knowledge, implement such method ugly way:

void printmsg(string type, string msg) {     system.out.println("exampleprogram -" + type + "- " + msg); } 

is there equivalent python's .format() string method?

messageformat has exact usage.

 int planet = 7;  string event = "a disturbance in force";   string result = messageformat.format(      "at {1,time} on {1,date}, there {2} on planet {0,number,integer}.",      planet, new date(), event); 

you can simple use {0123} without extras. output is:

 @ 12:30 pm on jul 3, 2053, there disturbance in force on planet 7. 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -