How to split long strings in IntelliJ IDEA automatically?

后端 未结 4 1677
迷失自我
迷失自我 2021-02-05 00:54

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


        
相关标签:
4条回答
  • 2021-02-05 01:36

    Same version - Community Edition.

    0 讨论(0)
  • 2021-02-05 01:44

    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.

    0 讨论(0)
  • 2021-02-05 01:45

    At least for me, using code formatting options still don't do this. Using 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.

    0 讨论(0)
  • 2021-02-05 01:53

    You can use auto-formatting (CTRL + ALT + L), after few changes in Code Style settings.

    • Press CTRL + ALT + S to open Settings window
    • Find Code Style / Java section
    • 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) long strings exceeding line character limit will be automatically cut into multiple lines.

    0 讨论(0)
提交回复
热议问题