Paste a multi-line Java String in Eclipse

JavaEclipseMultilineMultilinestring

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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"

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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"

Solution 5 - Java

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

Example

Paste as usual via Ctrl+V:

and new 
lines```

Paste as Java string via Ctrl+Shift+V

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

Solution 6 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThiloView Question on Stackoverflow
Solution 1 - JavaThiloView Answer on Stackoverflow
Solution 2 - Javauser1772710View Answer on Stackoverflow
Solution 3 - JavaBrianView Answer on Stackoverflow
Solution 4 - JavazzgView Answer on Stackoverflow
Solution 5 - JavaEnybyView Answer on Stackoverflow
Solution 6 - JavaRavishaView Answer on Stackoverflow