How to find a replace a word in a string in C#?

CsharpProgrammingServer Side Programming

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

 Live Demo

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!
raja
Published on 20-Aug-2018 12:56:44
Advertisements