Export (0) Print
Expand All

String.Remove Method (Int32, Int32)

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

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public string Remove (
	int startIndex,
	int count
)
public String Remove (
	int startIndex, 
	int count
)
public function Remove (
	startIndex : int, 
	count : int
) : String

Parameters

startIndex

The position 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.

Exception typeCondition

ArgumentOutOfRangeException

Either startIndex or count is less than zero.

-or-

startIndex plus count specify a position outside this instance.

The following is an example of C# code that prints "123456".

String s = "123abc456"; 
Console.WriteLine(s.Remove(3, 3));

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

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);
        }
    }
}

import System.*;

public class RemoveTest
{
    public static void main(String[] args)
    {
        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);
        }
    } //main
} //RemoveTest

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();

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft