In C#, String Replace In Depth

String Replace In Depth Banner Image

What is String Replace?

In C#, the string replace are 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 its 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 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 the 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 separated 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 replaces a string or multiple characters with another string. Since it is powerful, being specific is important that other areas of the string are not replaced accidentally. So it's best to use it when there are repeated patterns or even it can be used to remove characters. See the 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-separated list
string trafficTicketsMetricsUpdated = trafficTicketsMetrics.Replace("%", "percent");//replacing % for percent in all parts of the string
trafficTicketsMetricsUpdated = trafficTicketsMetricsUpdated.Replace("Unwanted#", "");//Replaced the text Unwanted# and replaced 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. Depending 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 a null or empty string as well.

StringComparision

See the below table to see the different descriptions for each of the StringComparision types.

StringComparision Type Description
CurrentCultureBased the equals on the current culture settings(default)
CurrentCultureIgnoreCaseBased on the current culture but case insensitive
InvariantCulture Based on English language rules but not any specific English speaking country
InvariantCultureIgnoreCaseBased on English language rules but case insensitive
OrdinalBased on tne binary of the characters. Known to be the fastest.
OrdinalIgnoreCaseLike the Ordinal use case but case insensitive

For more details and examples of 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-separated 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 replaced 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,
C# Programming for Unity Game Development Specialization

COURSERA

C# Programming for Unity Game Development Specialization

4.7 (2,230 reviews)

Beginner level

No previous experience is necessary

3-months at 10 hours a week (At your pace)

$59 / month or audit, and financial aid available

If you want to learn more about C# then check out this course on Coursera. It's a 3 month course to take you through the basics of C#.

You can get started with programming using C# and apply it to Unity games in this beginner-level program. Facilitated by the University of Colorado, the program offers a chance to grasp the basics of C# and utilize the knowledge to develop Unity games.

[Full Disclosure: As an affiliate, I receive compensation if you purchase through this link]

Start Your 7-day Free Trial
Get Latest Updates