How to Replace a Character with the String in C#?
Last Updated :
03 Dec, 2021
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:
- Declare a string
- Use Replace() function to replace the character(i.e., “A”) with string(i.e., “Geeks For Geeks”))
input_string.Replace("A", "Geeks For Geeks")
- Display the modified string
Example:
C#
using System;
class GFG{
public static void Main()
{
String input_string = "A portal in India" ;
Console.WriteLine( "Actual String : " + input_string);
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...