C# String Remove Method

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

 

In case, 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 string Remove() method to delete a characters from string based on the defined starting position in c# programming language.

 

C# String Remove Method Representation Diagram

 

If you observe above diagram, we removed all the characters from the defined string “Welcome to Tutlane”, starting from the position 5 using 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 given string.

 

public string Remove(int startIndex)

public string Remove(int startIndex, int length)

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

C# String Remove Method Example

Following is the example of using string Remove() method to delete a characters in 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 above example, we used a string Remove() method to remove a characters starting from the position 5 till the end of defined string and another Remove() method to delete a 7 characters starting from the position 3.

 

When we execute above c# program, we will get the result like as shown below.

 

C# String Remove Method Example Result

 

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

 

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