C# Program to replace a special character from a String

CsharpProgrammingServer Side Programming

Let’s say our string is −

string str = "abcd$ef$gh";

To replace the special character, use the Replace() method.

string res = str.Replace('$', 'k');

The following is the complete code to replace character from a string −

Example

 Live Demo

using System;
public class Program {
   public static void Main() {
      string str = "abcd$ef$gh";
      Console.WriteLine("Initial string = " + str);
      string res = str.Replace('$', 'k');
      // after replacing
      Console.WriteLine("Replaced string = " + res.ToString());
   }
}

Output

Initial string = abcd$ef$gh
Replaced string = abcdkefkgh
raja
Published on 21-Aug-2018 08:45:37
Advertisements