C# Program to replace a special character from a String


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements