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:
We need to replace x with Xray, o with option, and f with folder.
Here is a single line of code:
- str = str.Replace("x", "Xray").Replace("o", "option").Replace("f", "folder");
The output will be this:
- 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.