C# program to remove a range of characters by index using StringBuilder


Use the Remove() method to remove a range of characters by index.

Let’s say you need to remove the last 5 characters from the following string −

StringBuilder myStr = new StringBuilder("Framework");

For that, set the Remove() method as −

str.Remove(3, 4);

The following is the complete code −

Example

 Live Demo

using System;
using System.Text;

public class Program {
   public static void Main() {
      StringBuilder myStr = new StringBuilder("Framework");
      Console.WriteLine("Initial String: " + myStr);

      // removing four characters
      Console.Write("New string: ");
      myStr.Remove(5, 4);
      Console.WriteLine(myStr);
   }
}

Output

Initial String: Framework
New string: Frame

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

351 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements