String Replace() - C# Tutorial
C# Course / Strings / String Replace()

String Replace()

BLUF: Mastering String Replace() is essential for building robust applications with the .NET ecosystem. This tutorial provides clear explanations and practical examples to help you understand and apply this C# concept.
Enterprise Development Tip: String Replace()

C# is a powerful, modern language for enterprise solutions. Discover how String Replace() enhances your development workflow in the guide below.

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

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience