ReSharper
 
Get ReSharper
You are viewing the documentation for an earlier version of ReSharper.

Introduce Variable for Substring refactoring

Last modified: 21 July 2022

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.

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:

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.