javastringprintfapache-stringutils

Java Is there a feature for replacing placeholders inside a String with a String variable


I have a requirement to move all the hard-coded error logs to DB. Some of the error logs comes in the format

"xxxxx" + variable + "xxxxxx";

It is not possible to change the structure of the table. Neither can I do some String insert based on the position since the values in the DB can always change.

Is there a feature in any Java libraries or third party ones where you can have placeholders in the String value and they get replaced by variables. To quote an example, we could think of the good old

printf("bla %s %d bla,", var, var2);

or the SQL PreparedStatement class

stmt("select * from blah where a = ?"); 

And the '?' gets replaced by a variable content.

Any suggestions would be of great help.


Solution

  • JDK 7+ has String.format() which I what I think you're looking for