How to replace multiple strings in a given string in C#

Here is a quick tip on how to replace multiple strings in a give string or line or text using C# String.Replace method.

Some times we need to replace more than one string or character in a string. Rather than going through one by one and finding and replacing, we can use String.Replace method on the same string multiple times.
 
First method of replacing multiple strings from a string is find and replace one by one. 
 
Alternatively, we can use the Replace method in a single line. 
 
Here is a simple string: 
  1. string str="x/o/f";  
We need to replace x with Xray, o with option, and f with folder. 
 
Here is a single line of code: 
  1. str = str.Replace("x""Xray").Replace("o""option").Replace("f""folder");  
The output will be this: 
  1. str="Xray/option/folder";  
This may not be the effective way but you can write clean code.
 
Note: One thing you want to be careful with is, look for the order of replaced strings and what you trying to get out of it. For example, if you want to replace "o" with "option" and "op" with "operations", you need to be sure to look for the right sub string or characters. 
 
Alternatively, we can also use regular expressions. Here is a detailed article and code samples on Regular Expressions In C#.