C# Program to replace a character with asterisks in a sentence

CsharpProgrammingServer Side Programming

Use the Replace() method to replace a character with asterisks.

Let’s say our string is −

string str = "dem* text";

To replace it, use the Replace() method −

str.Replace('*', 'o');

Here is the complete code −

Example

 Live Demo

using System;
public class Program {
   public static void Main() {
      string str = "dem* text";
      Console.WriteLine("Initial string = " + str);
      string res = str.Replace('*', 'o');
      // after replacing
      Console.WriteLine("After replacing asterisk = " + res.ToString());
   }
}

Output

Initial string = dem* text
After replacing asterisk = demo text
raja
Published on 21-Aug-2018 08:50:11
Advertisements