This question already has an answer here:
- Surround with quotation marks 4 answers
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).
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"
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.
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.
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"
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"
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
来源:https://stackoverflow.com/questions/2159678/paste-a-multi-line-java-string-in-eclipse