C# Program to change a character from a string


Let's say our string is −

StringBuilder str = new StringBuilder();
str.Append("pre");

To change a character, set the value at that particular index. The following sets a character at the 3rd position −

str[2] = 'o';

Here is the complete code −

Example

 Live Demo

using System;
using System.Text;

public class Demo {
   public static void Main() {
      StringBuilder str = new StringBuilder();
      str.Append("pre");
      Console.WriteLine("String : "+str);
      Console.WriteLine("Change 3rd character");
      str[2] = 'o';
      Console.WriteLine("New String : "+str);
   }
}

Output

String : pre
Change 3rd character
New String : pro

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements