Since Java 15 String.format can be replaced with String#formatted.
Is there an easy way to replace this
String.format("%s foo %s bar", bar(), foo());
with this in IntellIJ IDEA Ultimate?
"%s foo %s bar".formatted(bar(), foo());
Since Java 15 String.format can be replaced with String#formatted.
Is there an easy way to replace this
String.format("%s foo %s bar", bar(), foo());
with this in IntellIJ IDEA Ultimate?
"%s foo %s bar".formatted(bar(), foo());
For this particular code you can use Regex replacement upon pressing ctrl+R in IntelliJ Idea and clicking the .*
icon.
String.format\((\".*\")?\, ?(.*)\);
$1.formatted\($2\);
I have to remind you multiple reasons not to do that:
formatted
method is comfortable because of a direct use. I describe this in my another answer.A better way to do if you wish so is to find these methods and replace them manually. In such case, do the full-text search using ctrl+shift+F and make the search based on this pattern:
String\.format
Press ctrl+shift+R to select and replace all occurrences of the text in project at one go.
Select Regex mode and Search Text -
String[.]format[(](.+?),
Replace Text -
$1.formatted\(
N.B I agree with the accepted answer. Regex replacement at large scale is not safe and reliable. I can think of few scenarios where if there is a slight change in the code, the regex replacement in this answer wouldn't work -
Comma inside the format expression
String.format("%s foo, %s bar", bar(), foo());
Extra spaces in between
String.format ( "%s foo, %s bar", bar(), foo());
Code separated by new lines
String.
format("%s foo %s bar", bar(), foo());
String.format("%s foo " +
"%s bar", bar(), foo());