Introduce Format Item
In This Article
Introduces a new format item to the string (e.g., “{0}”, “{1}”, etc.) which represents the selected text, and if necessary, creates a new String.Format call to surround the string. The selected text is moved out to become an argument to String.Format.
#Purpose
This refactoring allows you to make your code more flexible, especially when followed by Introduce Local or a similar refactoring. This allows you to replace a portion of a string with a value that can change dynamically.
#Availability
Available from the context menu or via shortcuts:
- when the caret is on a String.Format method call and a fragment of the first parameter is selected.
- when a part of a string constant is selected. In this case, a new String.Format method call is automatically generated.
#Notes
- This refactoring is the functional opposite of Inline Format Item.
- Use String.Format can also be useful when working with composite strings.
#Example
string str = "Hello │World";
Dim str As String = "Hello │World"
Result:
string str = String.Format("Hello {0}", │"World");
Dim str As String = String.Format("Hello {0}", │"World")
#Screenshot
See Also