52

I am writing a test with very long strings, and I need to split those strings:

private static final String TOO_LONG_JSON = "{field1:field1, field2:field2 ... fieldN:fieldN}";

so they would become:

private static final String TOO_LONG_JSON = "{field1:field1, field2:field2" + 
    "{field3:field3, field4:field4,field6:field6, field7:field7}" +
    "{field8:field8, field9:field9,field10:field10, field11:field11}" +
    " ... fieldN:fieldN}";

Is it possible to set up IntelliJ IDEA to automatically split those long strings?

5
  • Are you changing the content of the string? Commented Jun 24, 2014 at 1:21
  • 5
    No just split it with + sign. For example it possible to configure Intellin to wrap code line that more than 80 symbols, I need something similar with too long strings.
    – Cherry
    Commented Jun 24, 2014 at 1:24
  • 5
    Look into code formatting shortcuts. Commented Jun 24, 2014 at 1:26
  • 1
    I am looking, but what exactly I have to see? :)
    – Cherry
    Commented Jun 25, 2014 at 8:04
  • I agree with commenters below that the latest version of IntelliJ doesn't break the string with code formatting. However, you can move your cursor inside the string and press Enter to split the string manually.
    – bonh
    Commented Jan 2, 2019 at 16:56

5 Answers 5

46

You can use auto-formatting by running the Reformat code function (CTRL+ALT+L) after few changes in Code Style settings.

(alternatively Reformat code, ++L on MacOS),

  • Press CTRL+ALT+S to open Settings window (or Preferences, +, on MacOS)
  • Go to Code Style menu, and choose Java submenu
  • Go to Wrapping and Braces tab
  • Make sure to uncheck Line Breaks option and check Ensure right margin is not exceeded option
  • Press OK to accept changes you've made

Now when using auto-formatting (CTRL+ALT+L / MacOS ++L) long strings exceeding line character limit will be automatically cut into multiple lines.

8
  • 1
    @GiliGaribi Which version of IntelliJ IDEA are you using? I've just tested it out on the newest one (currently 2017.3.5) and surely it works. The "Line breaks" and "Ensure right margin is not exceeded" options are now under the "Wrapping and Braces" tab, but they still work as intended.
    – mkierc
    Commented Mar 15, 2018 at 9:55
  • 5
    This made the problem worse for me. It still doesn't break long strings, but now it also messes up other code. Using 2017.3.5
    – eis
    Commented Apr 13, 2018 at 7:21
  • 1
    Highlight the line with the string, if you don't wish to reformat the entire file
    – Born2Smile
    Commented Sep 23, 2019 at 11:26
  • I cannot find this in version 2020.2.1
    – Eponymous
    Commented Aug 27, 2020 at 23:57
  • 2
    2022.2 Ultimate - this answer just plain doesn't work.
    – cb4
    Commented Aug 8, 2022 at 22:53
3

Same version - Community Edition. Here's the relevant section

1
  • 10
    By the way, it's working for long lines in general, but not to one line with a long string Commented Mar 15, 2018 at 14:45
1

2022 UPDATE There is still no answer to what the OP asked - configure IntelliJ to "automatically split those long strings". The most upvoted answer does not work.

There is only one way (it's manual) to solve this problem short of someone writing a plugin. This method is guaranteed to work regardless of any other settings and provides 100% control of formatting of any code segment, not just those with long lines of strings.

Go to Settings Editor | Code Style | Formatter and enable the formatter on/off markers. Format the code fragment any way you want and surround it with the off/on phrases in code comments.

My example code segment:

System.out.println("This is a long line of text that includes " + variables() + " and " + some.otherStuff().toString() + ". But don't count on " + this.IDE().toName() + " to format it in " + anIntelligent() + " way. " + this.IDE().toName() + " should add a " + plusSign.Option() + " for long strings like this.");

RESULT 1 With settings as described in @mkierc's answer with a hard wrap at 80 per OP:

    System.out.println(
        "This is a long line of text that includes " + variables() + " in " +
                "it and " + some.otherStuff()
                                                                                             .toString() + ". But don't count on " + this.IDE()
                                                                                                                                             .toName() + " to format it in " + anIntelligent() + " way. " + this.IDE()
                                                                                                                                                                                                                    .toName() + " should add a " + plusSign.Option() + " for long strings like this.");

I don't know how this could be worse. The hard wrap was not honored after the second line and the ending semi-colon is at column 300! Honestly, I can't imagine what algorithm led to this mess. My son at 10 years old could do better...

RESULT 2 Formatted manually, margin at 80, and using formatter on/off markers:

//@formatter:off
System.out.println("This is a long line of text that includes " 
           + variables() + " in it and " + some.otherStuff().toString()
           + ". But don't count on " + this.IDE().toName() 
           + " to format it in " + anIntelligent() + " way. " 
           + this.IDE().toName() + " should add a " + plusSign.Option() 
           + " for long strings like this.");
//@formatter:on

It's possible that Result 1 can be improved with other settings. I tried a few seemingly relevant options and combinations thereof. None significantly changed the results.

1

At least for me, code formatting options still don't do this in 2020.2.3 Ultimate version. It seems to only split the string on punctuation marks - "fooofoooo.ffoooo" is broken to "fooofoooo" + "ffoooo" but long string with letters in it is not split.

Only thing I've seen working consistently is to place your cursor within the string and press enter. That's hardly "automatically", but best answer so far.

-1

I had to paste a long SQL String for a native query in my application. I think the real solution is to put your cursor inside the "" marks of this line of code typed manually: " " +

When you paste the LONG STRING of text inside the quotation marks, IntelliJ will format each line of code also with \t and \n inside, for you.

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.