在 Eclipse 中粘贴多行 Java 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2159678/
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-08-13 04:13:56  来源:igfitidea点击:

Paste a multi-line Java String in Eclipse

javaeclipsemultilinemultilinestring

提问by Thilo

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

不幸的是,Java 没有用于多行字符串文字的语法。如果 IDE 可以轻松处理类似的结构,那没问题

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

使用 Eclipse 将多行字符串从剪贴板粘贴到 Java 源代码中的最快方法是什么(以自动创建上述代码的方式)。

采纳答案by Thilo

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

好的,我刚刚找到了答案(在 Stackoverflow 上,不少于)。

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

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

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

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

回答by Ravisha

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

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

回答by Brian

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.

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

回答by user1772710

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

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

回答by 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"

回答by Enyby

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

EclipsePasteAsJavaString插件允许你插入的文本,通过按Ctrl + Shift + V Java字符串

Example

例子

Paste as usual via Ctrl+V:

像往常一样通过 Ctrl+V 粘贴:

some text with tabs and new lines

some text with tabs and new lines

Paste as Java string via Ctrl+Shift+V

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

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

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