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
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.
Press Control+Shift+R, and then choose Introduce Variable for Substring on the Refactor This menu.
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:
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.
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.