222

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

Roman C
  • 49,761
  • 33
  • 66
  • 176
Thilo
  • 257,207
  • 101
  • 511
  • 656

6 Answers6

423

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"

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 47
    It should be noted that after enabling the feature you still have to first write two quotation marks and then paste the text inside those marks. – Lycha Jul 29 '10 at 13:18
  • For NetBeans there is a plugin: http://plugins.netbeans.org/plugin/2748/?show=true – mjn May 15 '13 at 08:00
  • 14
    @BobbyEickhoff because it's not idempotent (if that word makes sense here). ie, if you have a string "asdf\"asdf" and you copy & paste the middle part you end up with "asdf\\\"asdf". in fact, i think what eclipse should do is have a new ctrl+shift+v shortcut that pastes escaped. – kritzikratzi Jun 06 '13 at 22:56
  • 3
    After doing such a paste, has anyone found a way to make auto-format leave the string alone? – user1809090 Mar 27 '14 at 14:11
  • @user1809090 adding comments after each line preserves the layout. Example: "SELECT"// + " AVG(value)"// + "FROM"// – Tomasz Apr 01 '14 at 19:30
  • 6
    `//@formatter:off [your code] //@formatter:on` – Michele Mariotti May 28 '14 at 10:59
  • The annoying thing is that this should not be an option in preferences. There are few people that will *always* want this turned on (e.g. when pasting pre-escaped strings such as regular expressions) or turned off (e.g. your example). It should be a special paste construct in the dropdown of `edit`. – Maarten Bodewes Feb 14 '16 at 20:22
  • Is there a way to stop it from putting \r\n at the end of each line? – Gustavo Jun 14 '17 at 14:02
11

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.

khr055
  • 28,690
  • 16
  • 36
  • 48
user1772710
  • 318
  • 3
  • 10
  • 2
    I installed this in Juno, restarted but it does nothing. The combo doesn't do anything and neither can I find an item in the context menu :-( – Daniel Gerson Jun 20 '13 at 20:03
4

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.

Brian
  • 13,412
  • 10
  • 56
  • 82
  • CNTL+M = Make code. In the View > Toad Options you can set what format the 'code' is in. Java for example is there. – checketts Feb 16 '12 at 21:53
3

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"
zzg
  • 61
  • 6
0

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"

Enyby
  • 4,162
  • 2
  • 33
  • 42
-10

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

Ravisha
  • 3,261
  • 9
  • 39
  • 66
  • 2
    Disagree. This is totally in scope of an IDE. After all, you can edit a String, and type enter in the middle of it and Eclipse creates a proper Java multi-line string. – Thilo Jan 29 '10 at 04:03
  • @Thilo ur question was to copy a string into an editor which was not in a format as per the IDE ,but still u want it to automatically make these arrangements for you!Which i belive is out of perview of the IDE. Tell me one thing what exactly is ur requirement.Why u want to copy string?so that we get clarity of problem.Pls edit question rather comment – Ravisha Jan 29 '10 at 05:35
  • 2
    I have found many use cases where you have large text values that you want to directly use in Java code (e.g. in a unit test where you want to try a method with very long input). So you will often need to paste a large, multi-line String such as a generated Lorem Ipsum text into the Java program. It seems to be in-scope for the IDE to support such an operation without forcing the user to manually break up the text and surround each line with quotes and +. – ammianus Apr 10 '13 at 18:45
  • @ammianus Having large strings in code itself is a bad practice IMHO. we have yml, properties files for that. you clearly need to distinguish what is code and what is data. – Ravisha Apr 02 '20 at 05:56