find and solve || findandsolve.com
Please wait.....

C# String Replace Method

In c#, 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.


C# String Replace Method Syntax

In the given below the syntax of defining a Replace method to replace a particular part of a character or a string in the c# programming language.

public string Replace(Char first, Char second)  
public string Replace(string firstString, string secondString)  

 If you observe syntax, the Replace method will replace all occurrences of first character with second character and  firstString with sescondString in a given string and return it as a new string.


C# String Replace Method Example

Following is the example of using the Replace() method to replace a particular part of a string or a character c# programming language.

Example
using System;   
    public class StringExample
    {
        public static void Main(string[] args)
        {
           string first = "Conssole Example in C#";
           string second = first.Replace('F','C');
           Console.WriteLine(second);
        }
    }   
output
Conssole Example in C#

Another Example

using System;   
    public class StringExample
    {
        public static void Main(string[] args)
        {
           string first = "Hello Find and solve";
           string second = first.Replace("Hello", "Welcome");
           Console.WriteLine("Old: {0}", first);
           Console.WriteLine("New: {0}", second);
           string x = "abc";
           string nx = x.Replace("a", "b").Replace("b", " ");
           Console.WriteLine("Old: {0}", x);
           Console.WriteLine("New: {0}", nx);
        }
    }    

output

Old: Hello Find and solve
New: Welcome Find and solve Old: abc
New: c