Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split long strings in IntelliJ IDEA automatically?

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?

like image 342
Cherry Avatar asked Jun 24 '14 01:06

Cherry


People also ask

How to split a long string?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.

How to split long string in Java?

Split() String method in Java with examples. The string split() method breaks a given string around matches of the given regular expression. After splitting against the given regular expression, this method returns a string array.

How to split one very long line in pycharm into multiple lines?

Mark the line, hit cmd+r and replace , by ,\n with ticks in Regex and Selection might be a work around.

How to split multiple lines in Java?

We can use the method splitlines() in string class to achieve this.


1 Answers

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.

like image 138
mkierc Avatar answered Sep 23 '22 20:09

mkierc