This documentation is archived and is not being maintained.

String.Remove Method

Deletes a specified number of characters from this instance beginning at a specified position.

[Visual Basic]
Public Function Remove( _
   ByVal startIndex As Integer, _
   ByVal count As Integer _
) As String
[C#]
public string Remove(
 int startIndex,
 int count
);
[C++]
public: String* Remove(
 int startIndex,
 int count
);
[JScript]
public function Remove(
   startIndex : int,
 count : int
) : String;

Parameters

startIndex
The position in this instance to begin deleting characters.
count
The number of characters to delete.

Return Value

A new String that is equivalent to this instance less count number of characters.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException Either startIndex or count is less than zero.

-or-

startIndex plus count is greater than the length of this instance.

Remarks

For example, the following C# code prints "123456".

String s

= "123abc456";

Console.WriteLine(s.Remove(3, 3));

Example

The following code example demonstrates how you can remove the middle name from a complete name.

[Visual Basic] 
Imports System

Public Class RemoveTest
    
    Public Shared Sub Main()
        Dim name As String = "Michelle Violet Banks"
                
        Console.WriteLine("The entire name is '{0}'", name)
        Dim foundS1 As Integer = name.IndexOf(" ")
        Dim foundS2 As Integer = name.IndexOf(" ", foundS1 + 1)
        If foundS1 <> foundS2 And foundS1 >= 0 Then
            
            ' remove the middle name, identified by finding the spaces in the middle of the name...    
            name = name.Remove(foundS1 + 1, foundS2 - foundS1)
            
            Console.WriteLine("After removing the middle name, we are left with '{0}'", name)
        End If
    End Sub 'Main
End Class 'RemoveTest

[C#] 
using System;

public class RemoveTest {
    public static void Main() {

        string name = "Michelle Violet Banks";
 
        Console.WriteLine("The entire name is '{0}'", name);

        // remove the middle name, identified by finding the spaces in the middle of the name...
        int foundS1 = name.IndexOf(" ");
        int foundS2 = name.IndexOf(" ", foundS1 + 1);

        if (foundS1 != foundS2 && foundS1 >= 0) {

            name = name.Remove(foundS1 + 1, foundS2 - foundS1);

            Console.WriteLine("After removing the middle name, we are left with '{0}'", name);
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

int main()
{
   String* name = S"Michelle Violet Banks";

   Console::WriteLine(S"The entire name is '{0}'", name);

   // remove the middle name, identified by finding the spaces in the middle of the name->->.
   int foundS1 = name->IndexOf(S" ");
   int foundS2 = name->IndexOf(S" ", foundS1 + 1);

   if (foundS1 != foundS2 && foundS1 >= 0)
   {
      name = name->Remove(foundS1 + 1, foundS2 - foundS1);
      Console::WriteLine(S"After removing the middle name, we are left with '{0}'", name);
   }
}

[JScript] 
import System;

public class RemoveTest {
    public static function Main() : void  {

        var name : String = "Michelle Violet Banks";
 
        Console.WriteLine("The entire name is '{0}'", name);

        // remove the middle name, identified by finding the spaces in the middle of the name...
        var foundS1 : int = name.IndexOf(" ");
        var foundS2 : int = name.IndexOf(" ", foundS1 + 1);

        if (foundS1 != foundS2 && foundS1 >= 0) {

            name = name.Remove(foundS1 + 1, foundS2 - foundS1);

            Console.WriteLine("After removing the middle name, we are left with '{0}'", name);
        }
    }
}
RemoveTest.Main();

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | Int32 | Concat | Insert | Join | Replace | Split | Substring | Trim

Show: