String.Replace()
Returns a new string in which all occurrences of a specified character or string in the current instance are replaced with another specified character or string.
string.Replace(char oldChar, char newChar)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Returns a new string in which all occurrences of a specified character or string in the current instance are replaced with another specified character or string.
The `String.Replace()` method in C# is used to substitute all occurrences of a specified character or substring within a string with another character or substring. It's important to remember that strings in C# are immutable, meaning this method does not modify the original string but instead returns a *new* string with the replacements applied. If the `oldValue` is not found in the string, the original string is returned unchanged. The time complexity for `String.Replace()` depends on the implementation but is generally proportional to the length of the string and the number of replacements, effectively O(N*M) in the worst case where N is string length and M is `oldValue` length, or closer to O(N) if the replacement is done efficiently. Edge cases include replacing with an empty string (effectively removing the `oldValue`), or replacing an empty string (no effect). Best practices involve using `String.Replace()` for simple, global substitutions. For more complex pattern-based replacements, `Regex.Replace()` is more appropriate. Ensure you assign the result of `Replace()` to a variable, as the original string remains unchanged.
Quick reference
Syntax
string.Replace(char oldChar, char newChar)
Inputs
Parameters
See it in practice
Examples
Replace a single character
string text = "Hello, World!";
string newText = text.Replace(',', '-');
Console.WriteLine(newText);Hello- World!
Replaces all commas in the string with hyphens.
Replace a substring
string sentence = "The quick brown fox jumps over the lazy dog.";
string newSentence = sentence.Replace("fox", "cat");
Console.WriteLine(newSentence);The quick brown cat jumps over the lazy dog.
Replaces the substring 'fox' with 'cat'.
Remove a substring by replacing with empty string
string data = "[ID:123] User Data";
string cleanedData = data.Replace("[ID:123] ", "");
Console.WriteLine(cleanedData);User Data
Removes a specific prefix by replacing it with an empty string.
Debug faster
Common Errors
Logical Error
Cause: Forgetting that strings are immutable and not assigning the result of Replace() to a variable.
Fix: Always assign the return value of String.Replace() to a variable, as the original string remains unchanged.
string original = "test";
original.Replace('t', 'x'); // original is still "test"
Console.WriteLine(original);Runtime support
Compatibility
Source: Microsoft Learn
Common questions
Frequently Asked Questions
Returns a new string in which all occurrences of a specified character or string in the current instance are replaced with another specified character or string.
oldValue: The character or string to be replaced. newValue: The character or string to replace all occurrences of `oldValue`.
Logical Error: Always assign the return value of String.Replace() to a variable, as the original string remains unchanged.