97

I'm using Intellij IDEA 10.5 Community. If I have a long String and want to split in in mulitple lines I press ENTER key in the middle of a String and get this:

String str = "ONE LONG" +
             "STRING";

Is it possible to put the + sign in the beginning of the line, like this:

String str = "ONE LONG"
             + "STRING";
4b0
  • 21,981
  • 30
  • 95
  • 142
kovica
  • 2,443
  • 3
  • 21
  • 25
  • LOL, forgive for offtopic, but why do you have plus symbol after the break? – sandalone Jun 20 '11 at 10:57
  • 3
    @sandalone I need this to 'cause at my current job all code is inspected with Sonar, and one of its annoying rules requires plus signs to be on a new line instead of at the end of the previous line... – Geert Bellemans Oct 31 '13 at 07:49
  • 19
    You may find it annoying, but I find it makes code far more readable. I can look at the beginning of the line and instantly know it's a continuation of the last (even if the end of the last line is off-screen). – Lambart Jan 23 '17 at 23:23
  • 4
    Also useful if you want to comment out certain lines of a multiline string in one step – Gene Bo Oct 11 '19 at 18:12
  • 4
    Why the default setting is to have it at the end of the line is beyond me. AFAIK, most style guides prefer it the other way around. Also, that's how plain old math works (i.e. operator repeated on the new line). – Priidu Neemre Jul 17 '20 at 14:37

4 Answers4

131

Settings (Preferences on macOS) | Editor | Code Style | Java | Wrapping and Braces | Binary expressions | Operation sign on next line:

Operation sign on next line

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • @KimballRobinson, file a bug at http://youtrack.jetbrains.net/issues/IDEA with the sample file and your code style exported with File | Export Settings. – CrazyCoder Jan 27 '12 at 04:12
  • Works today as well !. Options have not changed. Using IntelliJ Idea 2019.2 – VKB Mar 06 '20 at 20:41
  • I think @Armin's answer is more accurate nowadays - 2021.2 – Tom Carmi Feb 16 '22 at 18:51
  • @TomCarmi Thanks for the feedback, I've updated the answer with the screenshot and settings path from IntelliJ IDEA 2021.3.2. – CrazyCoder Feb 16 '22 at 19:44
51

In IntelliJ 15 this setting is in the preferences under

Editor > Code Style > Java > Wrapping and Braces (tab) > Binary Expressions (group) > Operation sign on next line (check box)

M. Justin
  • 14,487
  • 7
  • 91
  • 130
Armin
  • 1,367
  • 1
  • 12
  • 17
12

In 2016.3 only this helped me:

while in editor, click on menu : code->generate->tostring->settings->template tab-> copy "String concat (+)", this would allow you to edit a new template.

then in template paste this:

public java.lang.String toString() {
#if ( $members.size() > 0 )
#set ( $i = 0 )
    return "$classname{"
#foreach( $member in $members )
#if ( $i == 0 )
    + " ##
#else
    + ", ##
#end
#if ( $member.objectArray )
#if ($java_version < 5)
$member.name=" + ($member.accessor == null ? null : java.util.Arrays.asList($member.accessor)) +
#else
$member.name=" + java.util.Arrays.toString($member.accessor)
#end
#elseif ( $member.primitiveArray && $java_version >= 5)
$member.name=" + java.util.Arrays.toString($member.accessor) 
#elseif ( $member.string )
$member.name='" + $member.accessor + '\'' 
#else
$member.name=" + $member.accessor
#end
#set ( $i = $i + 1 )
#end
    + '}';
#else
    return "$classname{}";
#end
}
waypoint100
  • 191
  • 1
  • 8
1

In case someone is using Android Studio:

Android Studio > Settings > Editor > Code Style > Java

Click on tab "Wrapping and Braces" and then find the "Binary expressions" group. There you have the checkbox "Operation sign on next line"

giroxiii
  • 655
  • 5
  • 14