ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Introduce Variable for Substring refactoring

Last modified: 11 February 2024

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 more information about other languages, refer to corresponding topics in the Languages and frameworks section.