Firstly, set the string to be replaced.
string str = "Demo text!";
Now use the replace() method to replace the above string.
string res = str.Replace("Demo ", "New ");The following is the complete code to replace a word in a string.
Example
using System;
public class Demo {
public static void Main() {
string str = "Demo text!";
Console.WriteLine(str);
string res = str.Replace("Demo ", "New ");
Console.WriteLine("After replacing...");
Console.WriteLine(res);
}
}Output
Demo text! After replacing... New text!