ReSharper 2024.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.

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 Control+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 in C#

Feature is available in Visual Basic

Feature is not available in C++

Feature is not available in HTML

Feature is not available in ASP.NET

Feature is not available in Razor

Feature is available in JavaScript

Feature is available in TypeScript

Feature is not available in CSS

Feature is not available in XML

Feature is not available in XAML

Feature is not available in Resource files

Feature is not available in build script files

Feature is not available in Protobuf

Feature is not available in JSON

The instructions and examples given here address the use of the feature in C#. For more information about other languages, refer to corresponding topics in the ReSharper by language section.

Last modified: 11 February 2024