Structural Replace in String

Here's a simplified example of what I'm trying to do:

Convert:
String someText = "I like to eat bananas";

To:
final String BANANAS = "bananas";
String someText = "I like to eat " + BANANAS;

When I try to do execute the structural replace, I use:
Search Template = "$StringContent$"
Variable $StringContent$ = "bananas" (I've also tried ".*bananas" with no luck)
Replace Template = mypackage.MyClass.BANANAS

It always ends up like this:
final String BANANAS = "bananas";
String someText = BANANAS;

I know I can do this using a simple search/replace with regexes, but it doesn't give me the ability to:
--limit the search to Java Strings/comments easily
--Insert the fqn of the variable in the replace text and have it automatically shorten it for me.

This whole thing seems trivial to me. Is there something about the structural replace that I'm not getting?

Thanks in advance

2 comments
Comment actions Permalink

as I understand you, you need to capture only string content for that,
like following

String $someText$ = "I like to eat $bananas$";

Eric Martineau wrote:

Here's a simplified example of what I'm trying to do:

Convert:
String someText = "I like to eat bananas";

To:
final String BANANAS = "bananas";
String someText = "I like to eat " + BANANAS;

When I try to do execute the structural replace, I use:
Search Template = "$StringContent$"
Variable $StringContent$ = "bananas" (I've also tried ".*bananas" with no luck)
Replace Template = mypackage.MyClass.BANANAS

It always ends up like this:
final String BANANAS = "bananas";
String someText = BANANAS;

I know I can do this using a simple search/replace with regexes, but it doesn't give me the ability to:
--limit the search to Java Strings/comments easily
--Insert the fqn of the variable in the replace text and have it automatically shorten it for me.

This whole thing seems trivial to me. Is there something about the structural replace that I'm not getting?

Thanks in advance



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

Thanks for the response, I really appreciate it. I left out some important information:

The text "bananas" is plastered in Strings across the code base. So, in addition to the first example, I would also need to find/replace the following:

"The bananas were yellow" -> "The " + BANANAS + " were yellow"
"bananas contain potassium" -> BANANAS + " contain potassium"

0

Please sign in to leave a comment.