IntelliJ IDEA 2016.2 Help

Working with Strings in Scala

IntelliJ IDEA lets you add different intentions for strings and perform different actions with strings in Scala. IntelliJ IDEA also supports the Scala multi-line strings feature. This feature lets you format literals and helps your code to look more orderly.

Inserting Gap with Concatenation into a String

  1. In your editor, enter a string and press Alt + Enter. The list of appropriate intentions opens.
    gap_concatination
  2. Select Insert gap with concatenation ("+ +") and press Enter. Now you can insert a value into your string. You can evaluate the result in the Scala Console window.
    gap_concatination_result

Converting a String Concatenation into Interpolated String

For the Scala version 2.10 and later you have the ability to convert a string concatenation into the interpolated string.

  1. Select a value with concatenation in your string and press Alt + Enter. The list of appropriate intentions opens.
    interpolated_string
  2. Select Convert to interpolated string, press Enter and view the result.
    interpolated_string_result
  3. You can also choose an option Convert to formatted string and get your basic Java formatted string.

Converting a String into a Multi-line String and Vice Versa

  • Enter your string, press Alt + Enter and select Convert to """string""" option in the drop-down list of intentions.
    convert_to_string
    Converting to multi-line strings removes escaped sequences such as '\\' or '\n'.
  • You also can convert the multi-line string into the regular string. Press Alt + Enter to open the drop-down list of intentions. Select Convert to "string" and press Enter.
    convert_to_reg_string
    View the result.
    string_result
  • To start entering a multi-line string, simply type triple quotes in your editor. If you press Enter, it will automatically invoke stripMargin method. The stripMargin method removes the left-hand part of a multi-line string up to a specified delimiter.
    stripMargin_Method
    The white spaces are also preserved. Please see the following example:
    white_spaces_perserve

Inserting a Replace Intention

The replace("\r"," ") intention lets you keep the caret in the correct place on the next line in the multi-line strings regardless of what operating system you have at the moment.

  • Enter a multi-line string, press Alt + Enter and select the appropriate intention from the drop-down list.
    add_replace_intention
    replace_result

Inserting an Inject Language/Reference Intention

You can inject language or a reference into your multi-line string literals.

  • Enter a multi-line string, press Alt+ Enter and select Inject Language/Reference intention from the drop-down list. The list of available languages opens.
    lang_injection
  • Choose the appropriate one and press Enter.
    lang_intention
    To cancel the language injection, simply choose the Un-Inject Language/Reference intention.

Editing Multi-line String 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.

  1. In the main menu, select File | Setting | Editor | Code Style | Scala.
  2. On the Scala page, select the Multi-line strings tab.
    multi_line_strings
Last modified: 23 November 2016