In c#, the string Replace method is used to replace a specified string or a character in all occurrences of the given string.
The replace method will return a new string after replacing all occurrences of a specified string or a character.
Following is the pictorial representation of the replace method in the c# programming language.
If you observe the above diagram, we are replacing the “Hi” word in a given string “Hi Guest Hi” with “Welcome” using the Replace method. Once the replacement is done, the Replace method will return a new string like “Welcome Guest Welcome”.
Following is the syntax of defining a Replace method to replace a particular part of a string or a character in the c# programming language.
If you observe syntax, the Replace method will replace all occurrences of oldchar / oldstring with newchar / newstring in a given string and return it as a new string.
Following is the example of using the Replace() method to replace a particular part of a string or a character c# programming language.
If you observe the above example, we used a Replace() method to replace a particular part of a string or a character in a given string and return a new string.
When you execute the above c# program, you will get the result below.
This is how we can use Replace() method to replace a particular part of a string or a character in the c# programming language.