在 Eclipse 中粘贴多行 Java 字符串

发布于 2024-08-19 05:22:25 字数 252 浏览 7 评论 0原文

不幸的是,Java 没有多行字符串文字的语法。如果 IDE 可以轻松处理诸如

  String x = "CREATE TABLE TEST ( \n"
             + "A INTEGER NOT NULL PRIMARY KEY, \n"
            ...

使用 Eclipse 将多行字符串从剪贴板粘贴到 Java 源的最快方法是什么(以自动创建上述代码的方式)之类的构造,那就没问题了。

Unfortunately, Java has no syntax for multi-line string literals. No problem if the IDE makes it easy to work with constructs like

  String x = "CREATE TABLE TEST ( \n"
             + "A INTEGER NOT NULL PRIMARY KEY, \n"
            ...

What is the fastest way to paste a multi-line String from the clipboard into Java source using Eclipse (in a way that it automatically creates code like the above).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

与酒说心事 2024-08-26 05:22:25

好吧,我刚刚找到了答案(在 Stackoverflow 上,同样如此)。

Eclipse 有一个选项,将多行文本复制粘贴到字符串文字中将导致带引号的换行符:

Preferences/Java/Editor/Typing/“粘贴到字符串文字时转义文本”

Okay, I just found the answer (on Stackoverflow, no less).

Eclipse has an option so that copy-paste of multi-line text into String literals will result in quoted newlines:

Preferences/Java/Editor/Typing/ "Escape text when pasting into a string literal"

疏忽 2024-08-26 05:22:25

您可以使用此 Eclipse 插件:http://marketplace.eclipse.org/node/491839#.UIlr8ZDwCUm
这是一个多行字符串编辑器弹出窗口。将插入符号放入字符串文字中,按 ctrl-shift-alt-m 并粘贴文本。

You can use this Eclipse Plugin: http://marketplace.eclipse.org/node/491839#.UIlr8ZDwCUm
This is a multi-line string editor popup. Place your caret in a string literal press ctrl-shift-alt-m and paste your text.

吃素的狼 2024-08-26 05:22:25

如果您在 TOAD 或其他面向 SQL 的 IDE 等工具中构建 SQL,它们通常会将标记复制到剪贴板。例如,TOAD 有一个 CTRL+M,它可以在编辑器中获取 SQL,并完全执行上面代码中的操作。它还涵盖了相反的情况……当您从 Java 中获取格式化字符串并希望在 TOAD 中执行它时。将 SQL 粘贴回 TOAD 并执行 CTRL+P 以删除多行引号。

If your building that SQL in a tool like TOAD or other SQL oriented IDE they often have copy markup to the clipboard. For example, TOAD has a CTRL+M which takes the SQL in your editor and does exactly what you have in your code above. It also covers the reverse... when your grabbing a formatted string out of your Java and want to execute it in TOAD. Pasting the SQL back into TOAD and perform a CTRL+P to remove the multi-line quotes.

古镇旧梦 2024-08-26 05:22:25

请参阅: Multiple-line-syntax

它还支持以下变量多行字符串,例如:

String name="zzg";
String lines = ""/**~!{
    SELECT * 
        FROM user
        WHERE name="$name"
}*/;
System.out.println(lines);

输出:

SELECT * 
    FROM user
    WHERE name="zzg"

See: Multiple-line-syntax

It also support variables in multiline string, for example:

String name="zzg";
String lines = ""/**~!{
    SELECT * 
        FROM user
        WHERE name="$name"
}*/;
System.out.println(lines);

Output:

SELECT * 
    FROM user
    WHERE name="zzg"
杀手六號 2024-08-26 05:22:25

EclipsePasteAsJavaString 插件允许您通过 Ctrl + Shift + V

示例

粘贴 将文本作为 Java 字符串插入像往常一样通过 Ctrl+V:

一些带有制表符的文本
和新的
lines

通过 Ctrl+Shift+V 粘贴为 Java 字符串

"some text\twith tabs\r\n" +
“和新的\r\n”+
“线”

The EclipsePasteAsJavaString plug-in allows you to insert text as a Java string by Ctrl + Shift + V

Example

Paste as usual via Ctrl+V:

some text with tabs
and new
lines

Paste as Java string via Ctrl+Shift+V

"some text\twith tabs\r\n" +
"and new \r\n" +
"lines"

尐偏执 2024-08-26 05:22:25

据我所知,这似乎超出了 IDE 的范围。
Copyin ,您可以复制字符串,然后尝试使用 ctrl+shift+F 对其进行格式化
大多数情况下,这些多行字符串不使用硬编码,而是应从属性或 xml 文件中使用。可以在稍后的时间点进行编辑,无需更改代码

As far as i know this seems out of scope of an IDE.
Copyin ,you can copy the string and then try to format it using ctrl+shift+ F
Most often these multiline strings are not used hard coded,rather they shall be used from property or xml files.which can be edited at later point of time without the need for code change

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文