Eclipse 拆分长字符串的快捷方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/257251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:23:08  来源:igfitidea点击:

Eclipse Shortcut to Split Long Strings

eclipseeditorkeyboard-shortcuts

提问by Paul Croarkin

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:

是否有一个 Eclipse 快捷方式将其转换为:

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

回答by Jon Skeet

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

诚然,将 + 放在第一行的末尾而不是第二行的开头,如果您的风格指南要求后者,这会令人恼火,但如果您不挑剔,那就太好了:)

回答by Fylke

All the formatting templates in Eclipse will put the plus on the next row (which Ifind really annoying), so you can simply apply the code formatter and the plus will end up on the next row.

Eclipse 中的所有格式模板都会将加号放在下一行(觉得这很烦人),因此您可以简单地应用代码格式化程序,加号将在下一行结束。

回答by John Stoneham

There may be a Quick Fix (Ctrl+ 1) for this as well.

也可能有一个快速修复 ( Ctrl+ 1)。

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

我在 3.4 中惊讶地发现有 Quick Fixes 可以将基于 + 的字符串连接转换为StringBuilderor 的使用MessageFormat。杰出的!

回答by Tomá? Zálusky

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

您也可以使用正则表达式格式化代码。选择表达式,按 Ctrl+F 并使用:

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

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

Replace with: "\R$1\+ "

用。。。来代替: "\R$1\+ "

☑ Regular expressions

☑ 正则表达式