Are there any alternatives to Java's String.format which can cache the format String insead of requiring a format parse on each run? it would probably look something like this
Java的String.format是否有任何替代方法可以缓存每次运行时需要格式解析的格式String insead?它可能看起来像这样
Formatter formatter = new Formatter( "oh %s" );
formatter.format("my"); // oh my
3 个解决方案
#1
4
You could use the MessageFormat
class.
您可以使用MessageFormat类。
MessageFormat mf = new MessageFormat("oh {0}");
System.out.println(mf.format(new Object[] {"my"}));
System.out.println(mf.format(new Object[] {"this will do it!"}));
Output:
输出:
oh my
oh this will do it!
#2
4
You could look into MessageFormat you can create one instance for a pattern and use it like this:
您可以查看MessageFormat,您可以为模式创建一个实例,并像这样使用它:
MessageFormat messageFormat = new MessageFormat(pattern); // initialize once
messageFormat.format(arguments, new StringBuffer(), null).toString(); // use often
#3
-8
String.format()
can do the trick in a single line of code, try:
String.format()可以在一行代码中完成这个技巧,尝试:
String s = String.format("oh %s", "my"); // oh my
#1
4
You could use the MessageFormat
class.
您可以使用MessageFormat类。
MessageFormat mf = new MessageFormat("oh {0}");
System.out.println(mf.format(new Object[] {"my"}));
System.out.println(mf.format(new Object[] {"this will do it!"}));
Output:
输出:
oh my
oh this will do it!
#2
4
You could look into MessageFormat you can create one instance for a pattern and use it like this:
您可以查看MessageFormat,您可以为模式创建一个实例,并像这样使用它:
MessageFormat messageFormat = new MessageFormat(pattern); // initialize once
messageFormat.format(arguments, new StringBuffer(), null).toString(); // use often
#3
-8
String.format()
can do the trick in a single line of code, try:
String.format()可以在一行代码中完成这个技巧,尝试:
String s = String.format("oh %s", "my"); // oh my