Hasan Tuncay
Hasan Tuncay

Reputation: 1108

Intellij replace all occurances of stringbuilder.toString()

I would like to find all occurances of toString method that belongs to StringBuilder :

StringBuilder sb = new StringBuilder();
sb.append("sometext");
return sb.toString();

And replace it with:

StringBuilder sb = new StringBuilder();
sb.append("sometext");
return Optimizer.stat(sb.toString());

Of course the var "sb" is not always written the same way :-)

I tried IntelliJ Ultimate Structural Search and Replace. But I could not find a way to only get toString of StringBuilder class.

Many thanks.

Upvotes: 3

Views: 407

Answers (1)

Andy Turner
Andy Turner

Reputation: 140309

In Intellij structural search use the pattern:

return $sb$.toString();

replacing with

return Optimizer.stat($sb$.toString());

Now, hit "Edit Variables", select sb, and set "Expression type (regexp)" to StringBuilder.

Upvotes: 10

Related Questions