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
Formatter formatter = new Formatter( "oh %s" );
formatter.format("my"); // oh my
A composite format string consists of zero or more runs of fixed text intermixed with one or more format items. The fixed text is any string that you choose, and each format item corresponds to an object or boxed structure in the list.
A composite string is basically a record defined by an IOLIST statement consisting of variables and associated formats used to create the string. Assigning data to a composite string causes the elements defined in the IOList to be loaded with their associated values.
Avoid using String. format() when possible. It is slow and difficult to read when you have more than two variables.
You could use the MessageFormat
class.
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!
You could look into MessageFormat you can create one instance for a pattern and use it like this:
MessageFormat messageFormat = new MessageFormat(pattern); // initialize once
messageFormat.format(arguments, new StringBuffer(), null).toString(); // use often
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With