Open In App

How to Replace a Character with the String in C#?

Given a string, now our task is to replace a specified character with the string. This task is performed with the help of Replace() method. This method is used to replace all the Unicode characters with the specified string and return a modified string. 

Syntax:



string.Replace(character, new_string)

Here, string is the input string, a character is present in the string that is going to replace, and new_string is the string that replaces the character.



Examples:

Input: A portal in India
Replace A with Hello
Output: Hello portal in India

Input: Python is not equal to java
Replace is with was
Output: Python was not equal to java

Approach:

To replace a character with the specified string follow the following step:

input_string.Replace("A", "Geeks For Geeks")

Example:




// C# program to replace a character
// with a specified string
using System;
 
class GFG{
 
public static void Main()
{
     
    // Define string
    String input_string = "A portal in India";
 
    Console.WriteLine("Actual String : " + input_string);
 
    // Replace the string 'A' with 'Geeks For Geeks'
    Console.WriteLine("Replaced String: " +
         input_string.Replace("A", "Geeks For Geeks"));
}
}

Output:

Actual String : A portal in India
Replaced String: Geeks For Geeks portal in India
Article Tags :
C#