IntellijIDEA lets you perform different actions with strings in Scala. IntellijIDEA also supports a Scala multi-line strings feature. This feature lets you format literals and helps your code to look more orderly.
Adding Different Intentions for Strings in Scala
You can add a number of intentions in your strings in Scala.
How to Insert Gap with concatenation into a String
IntellijIDEA lets you insert a gap with concatenation intention. To do that, in your Editor enter a string and press Alt+ Enter. The list of appropriate intentions opens.
Select Insert gap with concatenation ("+ +") and press Enter.
Now you can insert a value into your string. You can evaluate the result in Scala Console.
How to Convert a String Concatenation into Interpolated String
For the Scala version 2.10 and later you also have the ability to convert a string concatenation into the interpolated string.
To do that, select a value with concatenation in your string and press Alt+ Enter. The list of appropriate intentions opens. You can also choose an option Convert to formatted string and get your basic formatted string.
Select Convert to interpolated string, press Enter and view the result.
How to Convert a Strings into a Multi-line String
You can convert your string into a multi-line string. Simply press Alt+ Enterand select Convert to """string""" option in the drop-down list of intentions.
You can also type triple quotes to enter multi-line strings in your editor.
If you press Enter, it will automatically envoke stripMargin method. The stripMargin method removes the left-hand part of a multiline string up to a specified delimiter.
The white spaces are also preserved. Please see the following example:
Selecting Intentions for Scala Multi-line Strings
You can choose different intention options for Scala multi-line strings. Place your cursor on the string and press Alt+ Enter.
For example, if you select Convert to "String" option, you will see the following result:
If you decide to convert it back to multi-line string, it will delete the escaped sequences such as '
' or '\n'.
The multi-line string is converted to the one-line string.
For examle, you can choose option Insert gap with concatenation. For the Scala version 2.10 and later you also have the ability to convert a string concatenation into the interpolated string.
Now you can convert the concatenation into the interpolated string.
The result is as follows:
Editing Multi-line Strings Settings
You can set a different format for multi-line strings options such as Margin char indent or Margin Char. You can also disable a multi-line strings support.