Menu
-->
See also See also
You can find and replace text in the Visual Studio editor by using Find and Replace (Ctrl+F or Ctrl+H) or Find/Replace in Files (Ctrl+Shift+F or Ctrl+Shift+H). You can also find and replace only some instances of a pattern by using multi-caret selection.
Tip
If you're renaming code symbols such as variables and methods, it's better to refactor them than to use find-and-replace. Refactoring is intelligent and understands scope, whereas find-and-replace blindly replaces all instances.
Find-and-replace functionality is available in the editor, in certain other text-based windows such as the Find Results windows, in designer windows such as the XAML designer and Windows Forms designer, and in tool windows.
You can scope searches to the current document, the current solution, or a custom set of folders. You can also specify a set of file name extensions for multi-file searches. Customize search syntax by using .NET regular expressions.
Tip
The Find/Command box is available as a toolbar control, but isn't visible by default. To display the Find/Command box, select Add or Remove Buttons on the Standard toolbar, and then select Find.
Find and Replace control
- Press Ctrl+F as a shortcut to find a string in the current file.
- Press Ctrl+H as a shortcut to find and replace a string in the current file.
The Find and Replace control appears in the upper right corner of the code editor window. It immediately highlights every occurrence of the given search string in the current document. You can navigate from one occurrence to another by choosing the Find Next button or the Find Previous button on the search control.
You can access replacement options by choosing the button next to the Find text box. To make one replacement at a time, choose the Replace Next button next to the Replace text box. To replace all matches, choose the Replace All button.
To change the highlight color for matches, choose the Tools menu, select Options, and then choose Environment, and select Fonts and Colors. In the Show settings for list, select Text Editor, and then in the Display items list, select Find Highlight (Extension).
Search tool windows
You can use the Find control in code or text windows, such as Output windows and Find Results windows, by selecting Edit > Find and Replace or pressing Ctrl+F.
A version of the Find control is also available in some tool windows. For example, you can filter the list of controls in the Toolbox window by entering text in the search box. Other tool windows that allow you to search their contents include Solution Explorer, the Properties window, and Team Explorer.
Find in Files and Replace in Files
- Press Ctrl+Shift+F as a shortcut to find a string in multiple files.
- Press Ctrl+Shift+H as a shortcut to find and replace a string in multiple files.
Find/Replace in Files works like the Find and Replace control, except that you can define a scope for your search. Not only can you search the current open file in the editor, but also all open documents, the entire solution, the current project, and selected folder sets. You can also search by file name extension. To access the Find/Replace in Files dialog box, select Find and Replace on the Edit menu (or press Ctrl+Shift+F).
Find Results
When you choose Find All, a Find Results window opens and lists the matches for your search. Selecting a result in the list displays the associated file and highlights the match. If the file is not already open for editing, it is opened in a preview tab in the right side of the tab well. You can use the Find control to search through the Find Results list.
Create custom search folder sets
You can define a search scope by choosing the Choose Search Folders button (it looks like ..) next to the Look in box. In the Choose Search Folders dialog box, you can specify a set of folders to search, and you can save the specification so that you can reuse it later.
Tip
If you've mapped a remote machine's drive to your local machine, you can specify folders to search on the remote machine.
Create custom component sets
You can define component sets as your search scope by choosing the Edit Custom Component Set button next to the Look in box. You can specify installed .NET or COM components, Visual Studio projects that are included in your solution, or any assembly or type library (.dll, .tlb, .olb, .exe, or .ocx). To search references, select the Look in references box.
Multi-caret selection
Note
This section applies to Visual Studio on Windows. For Visual Studio for Mac, see Block selection.
Introduced in Visual Studio 2017 version 15.8
Use multi-caret selection to make the same edit in two or more places at the same time. For example, you can insert the same text or modify existing text in multiple locations at the same time.
In the following screenshot,
-0000
is selected in three locations; if the user presses Delete, all three selections are deleted:To select multiple carets, click or make first text selection as usual, and then press Alt while you click or select text in each additional location. You can also automatically add matching text as additional selections, or select a box of text to edit identically on each line.
Tip
If you've selected Alt as the modifier key for mouse-click Go to Definition in Tools > Options, multi-caret select is disabled.
Commands
Use the following keys and actions for multi-caret selection behaviors:
Shortcut | Action |
---|---|
Ctrl+Alt + click | Add a secondary caret |
Ctrl+Alt + double-click | Add a secondary word selection |
Ctrl+Alt + click + drag | Add a secondary selection |
Shift+Alt+. | Add the next matching text as a selection |
Ctrl+Shift+Alt+, | Add all matching text as selections |
Shift+Alt+, | Remove last selected occurrence |
Ctrl+Shift+Alt+. | Skip next matching occurrence |
Alt + click | Add a box selection |
Esc or click | Clear all selections |
Some of the commands are also available on the Edit menu, under Multiple Carets:
See also
I have a (large) template and want to replace multiple values. The replacement needs to be done case insensitive. It must also be possible to have keys that does not exist in the template.
For example:
Note that if I have a list of 30 parameters and a large template, I do not want 30 large strings in memory. Using a StringBuilder seems to be an option, but other solutions are also welcome.
Solution I tried but did not work
Solution found here (C# String replace with dictionary) throws an exception when a key is not in the colletion, but our users makes mistakes and in that case I want to just leave the wromg key in the text. Example:
Community♦
Alex SiepmanAlex Siepman
4 Answers
And here is a 5000 iterations test benchmark, have not looked at memory or anything else.
Replacement function is the one you have checked as the accepted answer.
Community♦
PrixPrix16.2k1212 gold badges5656 silver badges115115 bronze badges
Using a StringBuilder seems to be an option, but other solutions are also welcome.
Since you want case insensitive, I'd suggest (non StringBuilder):
Erik PhilipsErik Philips42.6k66 gold badges9797 silver badges132132 bronze badges
I think I might have something you could try. I used something similar to it for email templates
Inside of your template you can have tags such as <% %> as place holders for the values you want
Hope this helps!
waltmagicwaltmagic42922 gold badges66 silver badges2020 bronze badges
The answer of Marc Gravell: C# String replace with dictionary can be changed an little bit so it does not throws an exception when the match can not be found. In this case it simply does not replace the match.
In case the string to be replace is tokenized, this is the solution:
I have tested the solution with a 100.000 bytes string, 7 keys and hundreds of replacements. It uses 7 times more memory then the lenght of the string. And it took only 0.002 seconds.
Quest: – Meet Mercer outside. Skyrim followers keep up quotes.
Community♦
Alex SiepmanAlex Siepman
Not the answer you're looking for? Browse other questions tagged c#stringbuilder or ask your own question.
-->Definition
Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.
Overloads
Replace(String, String, Boolean, CultureInfo)Replace(String, String, Boolean, CultureInfo)Replace(String, String, Boolean, CultureInfo)Replace(String, String, Boolean, CultureInfo) | |
Replace(Char, Char)Replace(Char, Char)Replace(Char, Char)Replace(Char, Char) | Returns a new string in which all occurrences of a specified Unicode character in this instance are replaced with another specified Unicode character. |
Replace(String, String)Replace(String, String)Replace(String, String)Replace(String, String) | Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string. |
Replace(String, String, StringComparison)Replace(String, String, StringComparison)Replace(String, String, StringComparison)Replace(String, String, StringComparison) |
Replace(String, String, Boolean, CultureInfo)Replace(String, String, Boolean, CultureInfo)Replace(String, String, Boolean, CultureInfo)Replace(String, String, Boolean, CultureInfo)
Parameters
- newValue
- StringStringStringString
- culture
- CultureInfoCultureInfoCultureInfoCultureInfo
Returns
Replace(Char, Char)Replace(Char, Char)Replace(Char, Char)Replace(Char, Char)
Returns a new string in which all occurrences of a specified Unicode character in this instance are replaced with another specified Unicode character.
Parameters
- oldChar
- CharCharCharChar
The Unicode character to be replaced.
- newChar
- CharCharCharChar
The Unicode character to replace all occurrences of
oldChar
.Returns
A string that is equivalent to this instance except that all instances of
oldChar
are replaced with newChar
. If oldChar
is not found in the current instance, the method returns the current instance unchanged.Examples
The following example creates a comma separated value list by substituting commas for the blanks between a series of numbers.
Remarks
This method performs an ordinal (case-sensitive and culture-insensitive) search to find
oldChar
. Note
This method does not modify the value of the current instance. Instead, it returns a new string in which all occurrences of
oldChar
are replaced by newChar
. Because this method returns the modified string, you can chain together successive calls to the Replace method to perform multiple replacements on the original string. Method calls are executed from left to right. The following example provides an illustration.
Concat(Object)Concat(Object)Concat(Object)Concat(Object)
Insert(Int32, String)Insert(Int32, String)Insert(Int32, String)Insert(Int32, String)
Join(String, String[])Join(String, String[])Join(String, String[])Join(String, String[])
Remove(Int32, Int32)Remove(Int32, Int32)Remove(Int32, Int32)Remove(Int32, Int32)
Match Case Word
Split(Char[])Split(Char[])Split(Char[])Split(Char[])
Substring(Int32)Substring(Int32)Substring(Int32)Substring(Int32)
Replace(String, String)Replace(String, String)Replace(String, String)Replace(String, String)
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
Parameters
- oldValue
- StringStringStringString
The string to be replaced.
- newValue
- StringStringStringString
The string to replace all occurrences of
oldValue
.Returns
A string that is equivalent to the current string except that all instances of
oldValue
are replaced with newValue
. If oldValue
is not found in the current instance, the method returns the current instance unchanged.Exceptions
ArgumentNullExceptionArgumentNullExceptionArgumentNullExceptionArgumentNullException
oldValue
is null
.ArgumentExceptionArgumentExceptionArgumentExceptionArgumentException
oldValue
is the empty string (').Examples
The following example demonstrates how you can use the Replace method to correct a spelling error.
Remarks
If
newValue
is null
, all occurrences of oldValue
are removed. How to make Popped Chorus Fruit in MinecraftThis Minecraft tutorial explains how to craft popped chorus fruit with screenshots and step-by-step instructions.In Minecraft, popped chorus fruit is one of the many food items that you can make, but you don't eat it to replenish your food meter. Popped chorus fruit is not made with a crafting table but rather with a.Let's explore how to add popped chorus fruit to your inventory. Supported PlatformsPopped chorus fruit is available in the following versions of Minecraft: PlatformSupported (Version.)Java Edition (PC/Mac)Yes Pocket Edition (PE)Yes Xbox 360Yes Xbox OneYes PS3Yes PS4Yes Wii UYes Nintendo SwitchYesWindows 10 EditionYes Education EditionYes. The version that it was added or removed, if applicable.NOTE: Pocket Edition (PE), Xbox One, Nintendo Switch, and Windows 10 Edition are now called Bedrock Edition. Instead, it is used to make.
Note
This method does not modify the value of the current instance. Instead, it returns a new string in which all occurrences of
oldValue
are replaced by newValue
. This method performs an ordinal (case-sensitive and culture-insensitive) search to find
oldValue
. Because this method returns the modified string, you can chain together successive calls to the Replace method to perform multiple replacements on the original string. Method calls are executed from left to right. The following example provides an illustration.
Concat(Object)Concat(Object)Concat(Object)Concat(Object)
Insert(Int32, String)Insert(Int32, String)Insert(Int32, String)Insert(Int32, String)
Join(String, String[])Join(String, String[])Join(String, String[])Join(String, String[])
Remove(Int32, Int32)Remove(Int32, Int32)Remove(Int32, Int32)Remove(Int32, Int32)
Split(Char[])Split(Char[])Split(Char[])Split(Char[])
Substring(Int32)Substring(Int32)Substring(Int32)Substring(Int32)
Replace(String, String, StringComparison)Replace(String, String, StringComparison)Replace(String, String, StringComparison)Replace(String, String, StringComparison)
Parameters
- newValue
- StringStringStringString
- comparisonType
- StringComparisonStringComparisonStringComparisonStringComparison
Returns
Applies to
I need to search in a string and replace a certain string
Ex: Search String 'Add Additional String to text box'. Replace 'Add' with 'Insert'
Replace Match Case C For Sale
Output expected = 'Insert Additional String to text box'
If you use string s='Add Additional String to text box'.replace('Add','Insert');
Output result = 'Insert Insertitional String to text box'
Have anyone got ideas to get this working to give the expected output?
Thank you!
DarshanaDarshana
6 Answers
You can use Regex to do this:
Extension method example:
Usage:
result: 'Insert Additional String to text box'
sa_ddam213sa_ddam21335.5k55 gold badges7777 silver badges9595 bronze badges
'bAddb' ensures that it will match the 'Add' which is not part of other words.Hope it's helpful.
Xiaodan MaoXiaodan Mao1,10622 gold badges1414 silver badges2424 bronze badges
Use the
string.Replace(string old, string replacement)
method.If you need greater flexibility use RegEx, but since you want to replace an exact string the string.replace method should suffice.
evanmcdonnalevanmcdonnal29.6k1111 gold badges6161 silver badges8484 bronze badges
If you want only to replace a full word, not correspondences inside another, you could do something like this:
Can't test now, but this may work.
João SimõesJoão Simões1,11311 gold badge1010 silver badges1919 bronze badges
Samjin MahadevanSamjin Mahadevan
answer for:
'This solution doesnt work if I need to replace a word that starts with @. Fiddle here dotnetfiddle.net/9kgW4h How can I get this working in this scenario. – Frenz Jan 16 '17 at 5:46'
'This solution doesnt work if I need to replace a word that starts with @. Fiddle here dotnetfiddle.net/9kgW4h How can I get this working in this scenario. – Frenz Jan 16 '17 at 5:46'
possible solution:
String Replace Ignore Case Java
I. BespalovI. Bespalov