A more elegant way could be:
String value = "Testing"; String template = "text goes here %s more text"; String result = String.format(template, value);
Or, alternatively, using MessageFormat:
String template = "text goes here {0} more text"; String result = MessageFormat.format(template, value);
Note that if you do this for logging, you can avoid the expense of doing this when the log line is below the threshold. For example, SLFJ :
The next two lines will lead to the same result. However, the second form will be superior to the first form by at least 30 times in the case of a disabled journal entry.
logger.debug("The new entry is "+entry+"."); logger.debug("The new entry is {}.", entry);
toolkit Apr 21 '09 at 15:05 2009-04-21 15:05
source share