views:

21

answers:

2

Say I've got an existing constant string:

private final static String LOREM_IPSUM = "Lorem Ipsum";

Is there a way in Eclipse to quickly extract a substring of this as another constant such that I could end up with something like:

private final static String LOREM = "Lorem";
private final static String IPSUM = "Ipsum";
private final static String LOREM_IPSUM = LOREM + " " + IPSUM;

In this particular case, two refactorings (one for LOREM and one for IPSUM) would suffice.

+1  A: 

Try this sequence of steps:

  1. Put your cursor after the m in Lorem, and press enter.
  2. Right cursor one character.
  3. Press enter.
  4. Select "Lorem" and perform Refactor -> Extract Constant...
  5. Select "Ipsum" and perform Refactor -> Extract Constant...
ChrisH
+1  A: 

There's a Quick Assist that you can use to pull out one bit of a quoted string. Select the text you want to pull out and hit Ctrl-1 (that's the digit for "one"). You'll see a quick assist for "Pick out selected part of String". Choose it; Eclipse will break up your string for you.

If you don't already use it, get familiar with the "Select Enclosing Element" key combination (Shift-Alt-Up). If you put the cursor in the middle of a string and hit that combination, the whole string will be selected. Select it again, and the expression containing it will be selected. You will do this dozens of times each day.

Ladlestein
Cool. I knew about Shift-Alt-Up, but not about "Pick out selected part of String". Always looking for good short-cuts.
Paul Croarkin