ReSharper 2018.3 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, ReSharper either uses interpolated string or String.Format(). Note that you can always convert between those with context actions (Alt+Enter).

To 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, e.g. an expression, a local variable initializer, a method argument, etc.

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

  3. ReSharper 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 ReSharper 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, use the Inline Parameter refactoring to move the variable to the method parameter.

Introducing variable for substring

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available

Feature is available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is available

Feature is available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.

Last modified: 25 April 2019

See Also