JetBrains Rider 2022.2 Help

Introduce Variable for Substring refactoring

This refactoring helps you quickly move a part of a string to a separate variable.

Note that the initial string should not be a type member assignment. The refactoring will create a local variable and therefore it only works where statements are allowed.

Depending on the target C# version, JetBrains Rider either uses interpolated string or String.Format(). Note that you can always convert between those with context actions Alt+Enter.

Introduce a variable for a substring

  1. Select a substring of a string in your code. The initial string should be where statement is allowed, for example an expression, a local variable initializer, a method argument, and so on.

  2. Press Ctrl+Alt+Shift+T, and then choose Introduce Variable for Substring on the Refactor This menu.

  3. JetBrains Rider moves the substring to a new variable and deploys a Hot spot session where you can choose variable type and name.

    You can use the following keys to go through the session parameters:

    • If JetBrains Rider suggests some values for the current parameter, use Up and Down arrow keys to navigate through the list of suggested values, or just type in a desired value.

    • Press Tab or Enter to accept the value and move to the input position of the next parameter. If this is the last parameter, the hot spot session completes and the caret moves to the end position defined for the session.

    • Press Shift+Tab to move the input focus to the input position of the previous parameter.

    • Press Esc to exit the hot spot session. In this case, all session parameters will be initialized with default values.

In the example below, the refactoring creates a variable for a part of the returned phrase:

Introducing variable for substring

Now you can vary this part of the phrase as necessary, for example, move the variable to the method parameter.

Introducing variable for substring
Last modified: 30 September 2021