C# program to remove n-th character from a string

CsharpProgrammingServer Side Programming

To remove a character, use the remove() method and set the index from where you want to delete the character.

Firstly, set the string.

string str1 = "Amit";
Console.WriteLine("Original String: "+str1);

To delete a character at position 4.

StringBuilder strBuilder = new StringBuilder(str1);
strBuilder.Remove(3, 1);

You can try to run the following code to remove nth character from a string.

Example

 Live Demo

using System;
using System.Text;
public class Demo {
   public static void Main(string[] args) {
      string str1 = "Amit";
      Console.WriteLine("Original String: "+str1);
      StringBuilder strBuilder = new StringBuilder(str1);
      strBuilder.Remove(3, 1);
      str1 = strBuilder.ToString();
      Console.WriteLine("String after removing a character: "+str1);
   }
}

Output

Original String: Amit
String after removing a character: Ami
raja
Published on 17-Sep-2018 12:11:08
Advertisements