C# .Remove()

LilyS_24's avatar
Published Nov 8, 2024
Contribute to Docs

In C#, the .Remove() method removes characters from a specific position in a string, starting at a given index. The original string remains unchanged, and the modified string is returned.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
    • Beginner Friendly.
      15 hours

Syntax

// Removes characters from startIndex to the end of the string.
string.Remove(int startIndex);

Or, alternatively:

// Removes a specified number of characters starting at startIndex.
string.Remove(int startIndex, int count);
  • startIndex: The zero-based position in the string where removal begins.
  • count (Optional): The number of characters to remove from the specified startIndex.

Example

The following example shows how to use the .Remove() method:

using System;
public class RemoveExample
{
public static void Main()
{
string baseStr = "ABCDEFGHIJ";
string newStr1 = baseStr.Remove(5);
string newStr2 = baseStr.Remove(2, 5);
Console.WriteLine("New string1: " + newStr1);
Console.WriteLine("New string2: " + newStr2);
}
}

This example results in the following output:

New string1: ABCDE
New string2: ABHIJ

Codebyte Example

Run the following codebyte example to understand how the .Remove() method works:

Code
Output

All contributors

Contribute to Docs

Learn C# on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
    • Beginner Friendly.
      15 hours