Eclipse Shortcut to Split Long Strings

徘徊边缘 提交于 2019-11-26 21:55:13

问题


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";

回答1:


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 :)




回答2:


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.




回答3:


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!




回答4:


Also you can format code using regular expression. Select expression, press Ctrl+F and use:

Find: "\s*?\+\s*?\R(\s*?)"

Replace with: "\R$1\+ "

☑ Regular expressions



来源:https://stackoverflow.com/questions/257251/eclipse-shortcut-to-split-long-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!