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


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

546 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements