The Replace function in C# is employed to generate a fresh string where every instance of a designated Unicode character within the string is substituted with a different designated Unicode character.
There are two approaches to utilizing the Replace method. It is possible to substitute strings as well.
Signature
Example
public string Replace(Char first, Char second)
public string Replace(String firstString, String secondString)
Parameter
first: it is a first parameter of char type.
second: it is a second parameter of char type.
Return
It returns a string.
C# String Replace Method Example
Example
using System;
public class StringExample
{
public static void Main(string[] args)
{
string s1 = "Hello F#";
string s2 = s1.Replace('F','C');
Console.WriteLine(s2);
}
}
Output:
Output
Hello C#
C# String Replace Method Example 2
Example
using System;
public class StringExample
{
public static void Main(string[] args)
{
string s1 = "Hello C#, Hello .Net, Hello C# Tutorial";
string s2 = s1.Replace("Hello","Cheers");
Console.WriteLine(s2);
}
}
Output:
Output
Cheers C#, Cheers .Net, Cheers C# Tutorial