Saturday, February 5, 2011

Eclipse Shortcut to Split Long Strings

I swear I've seen someone do this, but I can't find it in the various lists of shortcuts.

Given:

String s = "A very long ............................ String";

Is there an Eclipse shortcut to turn it into:

String s = "A very long ............................ " 
    + "String";
  • Yup - just hit return when your cursor is in the middle of the string.

    Admittedly that puts the + at the end of the first line instead of the start of the second, which is irritating if your style guide demands the latter, but if you're not fussy it's great :)

    Paul Croarkin : Too easy! Thanks!
    Dana the Sane : I wonder if there's a configuration setting somewhere to change the formatting?
    Jon Skeet : @Dana: I've tried, but can't find anything :(
    From Jon Skeet
  • All the formatting templates in Eclipse will put the plus on the next row (which I find really annoying), so you can simply apply the code formatter and the plus will end up on the next row.

    Jon Skeet : Hmm... that's not my experience. Will have to try again. Given that our coding conventions say to have the + on the next line (which makes a fair amount of sense, IMO) it would be helpful if you're right...
    Paul Croarkin : Just tried it. With my settings the + ends up on the next line after a ctrl+SHIFT-F
    From Fylke
  • There may be a Quick Fix (Ctrl+1) for this as well.

    I was amazed in 3.4 to discover that there are Quick Fixes to transform +-based string concats into uses of StringBuilder or MessageFormat. Brilliant!

0 comments:

Post a Comment