Open In App

C# | Replace() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In C#, Replace() method is a string method. This method is used to replace all the specified Unicode characters or specified string from the current string object and returns a new modified string. This method can be overloaded by passing arguments to it.

Syntax:

public string Replace(char Oldchar, char Newchar)
or
public string Replace(string Oldvalue, string Newvalue)

Explanation:
The First Method takes two parameters Oldchar and Newchar, where Oldchar is the Unicode character to be replaced and Newchar is the character to replace all occurrences of OldChar.
The second method also takes two parameters Oldvalue and Newvalue where Oldvalue is the string to be replaced and Newvalue is a string to replace all occurrences of Oldvalue. The return type value of both the methods is System.String.

Exceptions:

  • ArgumentNullException: If OldValue or Oldchar both are null.
  • ArgumentException If OldValue or Oldchar is the empty string (“”).

Below are the programs to demonstrate the above methods :

  • Example 1: Program to demonstrate the public string Replace(char Oldchar, char Newchar) method. All occurrences of a specified character are replaced with another specified character. If oldChar is not found in the current string object then string remains unchanged.
    Input : str  = "GeeksForGeeks"
            str.Replace('s', 'G');
    Output: GeekGForGeekG
    
    Input : str  = "GeeksForGeeks"
            str.Replace('e', ' ');
    Output: G  ksForG  ks
    




    // C# program to illustrate the Replace()
    // Method with character parameter
    using System;
      
    class Geeks {
      
        // Main Method
        public static void Main()
        {
      
            // string
            String str = "Geeks For Geeks";
      
            Console.WriteLine("OldString : " + str);
      
            // replace the character 's' with 'G'
            Console.WriteLine("NewString: " + str.Replace('s', 'G'));
      
            // oldString will remain unchanged
            // its return the modified string
            Console.WriteLine("\nOldString: " + str);
      
            // replace the character 'e' with space ' '
            Console.WriteLine("NewString: " + str.Replace('e', ' '));
        }
    }

    
    

    Output:

    OldString : Geeks For Geeks
    NewString: GeekG For GeekG
    
    OldString: Geeks For Geeks
    NewString: G  ks For G  ks
    
  • Example 2: Program to demonstrate the public string Replace(string Oldvalue, string Newvalue) method. All occurrences of a specified string in the current string instance are replaced with another specified string. If Oldvalue is not found in the current string then string remains unchanged.
    Input:  str  = "Geeks For Geeks"
            str.Replace("Geeks", "---");
    Output: --- For ---
    
    Input:  str  = "Geeks For Geeks"
            str.Replace("For", "GFG");
    Output: Geeks GFG Geeks
    




    // C# program to illustrate the Replace
    // Method with string parameter
    using System;
      
    class Geeks {
      
        // Main Method
        public static void Main()
        {
      
            // define string
            String str = "Geeks For Geeks";
      
            Console.WriteLine("OldString : " + str);
      
            // replace the string 'Geeks' with '---'
            // in string 'Geeks comes two time so replace two times
            Console.WriteLine("NewString: " + str.Replace("Geeks", "---"));
      
            // oldString will remain unchanged
            // its return the modified string
            Console.WriteLine("\nOldString: " + str);
      
            // replace the string 'For' with 'GFG'
            Console.WriteLine("NewString: " + str.Replace("For", "GFG"));
        }
    }

    
    

    Output:

    OldString : Geeks For Geeks
    NewString: --- For ---
    
    OldString: Geeks For Geeks
    NewString: Geeks GFG Geeks
    

To perform multiple replacements operations on the string(Replacement’s Chain) :

The above Replace() method returns the modified string, so now we can chain together successive calls to the Replace method to perform multiple replacements on the string. Method calls are executed from left to right.
In below example, for the given string “XXXXX” first X will be replaced with Y and then Y will be replaced with Z and finally, Z will be replaced with A.

Example :




// C# program to demonstrate the 
// multiple replacements calls
using System;
  
public class Geeks{
  
    // Main Method
    public static void Main()
    {
        String str = "XXXXX";
        Console.WriteLine("Old String: " + str);
  
        // chain together
        str = str.Replace('X', 'Y').Replace('Y', 'Z').Replace('Z', 'A');
        Console.WriteLine("New string: " + str);
    }
}


Output:

Old String: XXXXX
New string: AAAAA

Important Points to Remember :

  • Replace() method does not modify the value of the current instance. Instead, it returns a new string in which all occurrences of Oldvalue are replaced by Newvalue, similarly oldchar are replaced by Newchar.
  • It performs a case-sensitive search to find OldValue or Oldchar. If Newvalue is null, all occurrences of Oldvalue are removed.

References:



Last Updated : 10 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads