C# Program to change a character from a string

CsharpProgrammingServer Side Programming

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
raja
Published on 04-Sep-2018 11:17:14
Advertisements