- Strings
- String Replace In Depth
String Replace In Depth

What is String Replace?
String replace is a string and character substitute with another string or character. It finds the provided characters then removes those characters and inserts a new set of characters in it's place. This function replaces all characters that it can find, which is powerful so it's important to use precise text for it to find and replace.
Syntax
Function Overload 1
public string Replace(char findChar, char replaceChar);
This first overload takes in the first parameter finds the all the times that the character exists in the string and replaces it with the second parameter character. Use single quotes or char variables to pass to this function. See example below.
string trafficTicketsMetrics = "Speeding Ticket Count,Failure To Obey Traffic Sign Count,Traffic Stops Total,Speeding Ticket %,Failure To Obey Traffic Sign %";//Starting text with a header of a comma seperated list
string trafficTicketsMetricsUpdated = trafficTicketsMetrics.Replace('%', '!');//replacing % for !
Console.WriteLine("Original:" + trafficTicketsMetrics);//Print the original string to screen
Console.WriteLine("Updated:" + trafficTicketsMetricsUpdated);//Print the update string to screen
Code Output
Original:Speeding Ticket Count,Failure To Obey Traffic Sign Count,Traffic Stops Total,Speeding Ticket %,Failure To Obey Traffic Sign %
Updated:Speeding Ticket Count,Failure To Obey Traffic Sign Count,Traffic Stops Total,Speeding Ticket !,Failure To Obey Traffic Sign !
Function Overload 2
public string Replace(string findText, string? replaceText);
The most commonly used function overload that replaces a string or multiple characters with another string. Since is is powerful, being specific is important that other areas of the string are not replaced accidentally. So it's best to use when there are repeated patterns or even it can be used to remove characters. See example below.
string trafficTicketsMetrics = "Speeding Ticket Count,Failure To Obey Traffic Sign Count,Traffic Stops Total,Speeding Ticket %,Failure To Obey Traffic Sign %,Unwanted#";//Starting text with a header of a comma seperated list
string trafficTicketsMetricsUpdated = trafficTicketsMetrics.Replace("%", "percent");//replacing % for percent in all parts of the string
trafficTicketsMetricsUpdated = trafficTicketsMetricsUpdated.Replace("Unwanted#", "");//Replaced the text Unwanted# and replacing it with an empty string effectively just removing the text
Console.WriteLine("Original:" + trafficTicketsMetrics);//Print the original string to screen
Console.WriteLine("Updated:" + trafficTicketsMetricsUpdated);//Print the update string to screen
Code Output
Original:Speeding Ticket Count,Failure To Obey Traffic Sign Count,Traffic Stops Total,Speeding Ticket %,Failure To Obey Traffic Sign %,Unwanted#
Updated:Speeding Ticket Count,Failure To Obey Traffic Sign Count,Traffic Stops Total,Speeding Ticket percent,Failure To Obey Traffic Sign percent,
Function Overload 3
public string Replace(string findText, string? replaceText, StringComparison stringComparison);
findText
This is the text that you want to find. Depnding on which StringComparsion you pick it will be case or case insensitive.
replaceText
This is what you want to replace the findText with. It can be null or empty string as well.
StringComparision
See below table to see the different descriptions for each the StringComparision types.
StringComparision Type | Description |
---|---|
CurrentCulture | Based the equals on the current culture settings(default) |
CurrentCultureIgnoreCase | Based on the current culture but case insensitive |
InvariantCulture | Based on english language rules but not any specific english speaking country |
InvariantCultureIgnoreCase | Based on english language rules but case insensitive |
Ordinal | Based on binary of the characters. Known to be the fastest. |
OrdinalIgnoreCase | Like the Ordinal use case but case insensitive |
For more details and examples for the StringComparision, then please check out the following article:Examining String Equals In Detail
Function Overload 3 Code Example
string trafficTicketsMetrics = "Speeding Ticket Count,Failure To Obey Traffic Sign Count,Traffic Stops Total,Speeding Ticket %,Failure To Obey Traffic Sign %,Unwanted#";//Starting text with a header of a comma seperated list
string trafficTicketsMetricsUpdated = trafficTicketsMetrics.Replace("%", "percent", StringComparison.CurrentCulture);//replacing % for percent in all parts of the string
trafficTicketsMetricsUpdated = trafficTicketsMetricsUpdated.Replace("Unwanted#", "");//Replaced the text Unwanted# and replacing it with an empty string effectively just removing the text
Console.WriteLine("Original:" + trafficTicketsMetrics);//Print the original string to screen
Console.WriteLine("Updated:" + trafficTicketsMetricsUpdated);//Print the update string to screen
Code Output
Original:Speeding Ticket Count,Failure To Obey Traffic Sign Count,Traffic Stops Total,Speeding Ticket %,Failure To Obey Traffic Sign %,Unwanted#
Updated:Speeding Ticket Count,Failure To Obey Traffic Sign Count,Traffic Stops Total,Speeding Ticket percent,Failure To Obey Traffic Sign percent,