C# String Remove Method

In c#, the string Remove method is used to delete characters in the string starting from the specified position and continuing to the end of the string.

 

If we want to delete a particular length of substring, then we need to specify the starting position and length of characters to delete from the given string.

 

Following is the pictorial representation of using the string Remove() method to delete characters from the string based on the defined starting position in the c# programming language.

 

C# String Remove Method Representation Diagram

 

If you observe the above diagram, we removed all the characters from the defined string “Welcome to Tutlane”, starting from position 5 using the Remove method.

C# String Remove Method Syntax

Following is the syntax of defining a string Remove method to remove the characters starting from the defined position in a given string.

 

public string Remove(int startIndex)
public string Remove(int startIndex, int length)

If you observe syntaxes, the first syntax is used to delete all the string characters, starting from the defined position (startIndex). The second syntax is used to delete the particular length of a substring by defining the starting position and length of characters.

C# String Remove Method Example

Following is the example of using a string Remove() method to delete characters in a string based on the defined position in c# programming language.

 

using System;

namespace Tutlane
{
    class Program
    {
       static void Main(string[] args)
       {
          string msg = "Welcome to Tutlane";
          Console.WriteLine("Remove Result: {0}", msg.Remove(5));
          Console.WriteLine("Remove with Length: {0}", msg.Remove(3, 7));
          Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}

If you observe the above example, we used a string Remove() method to remove characters starting from position 5 till the end of the defined string and another Remove() method to delete 7 characters starting from position 3.

 

When you execute the above c# program, you will get the result below.

 

C# String Remove Method Example Result

 

If you observe the above result, the string Remove() method has deleted characters based on the defined positions and returned the remaining string characters.

 

This is how you can use the string Remove() method to delete the required characters from the given string based on the defined position in the c# programming language.