.Remove()
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.
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 specifiedstartIndex
.
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: ABCDENew string2: ABHIJ
Codebyte Example
Run the following codebyte example to understand how the .Remove()
method works:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C#
Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.Beginner Friendly23 hours