C# String Remove()

The String Remove() method removes a specified number of characters from the string.

Example

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
     
      string str = "Ice cream";
     
// removes characters from index 2 string result = str.Remove(2);
Console.WriteLine(result); Console.ReadLine(); } } } // Output: Ic

Remove() Syntax

The syntax of the string Remove() method is:

Remove(int startIndex, int count)

Here, Remove() is a method of class String.


Remove() Parameters

The Remove() method takes the following parameters:

  • startIndex - index to begin deleting characters
  • count (optional) - number of characters to delete

Remove() Return Value

The Remove() method returns:

  • string after removing characters

Example 1: C# String Remove()

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
     
      string str = "Chocolate";
     
// removes characters from index 5 string result = str.Remove(5);
Console.WriteLine(result); Console.ReadLine(); } } }

Output

Choco

Here,

  • str.Remove(5) - removes all characters from index 5

Example 2: C# String Remove() With Count

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
     
      string str = "Chocolate";
     
// removes 2 chars from index 5 string result = str.Remove(5, 2);
Console.WriteLine(result); Console.ReadLine(); } } }

Output

Chocote

Here,

  • str.Remove(5, 2) - removes 2 characters ('l' and 'a') from index 5
Did you find this article helpful?