Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a new string in which a specified number of characters from the current string are deleted.
Remove(Int32, Int32) |
Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted. |
Remove(Int32) |
Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted. |
Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.
public:
System::String ^ Remove(int startIndex, int count);
public string Remove(int startIndex, int count);
member this.Remove : int * int -> string
Public Function Remove (startIndex As Integer, count As Integer) As String
The zero-based position to begin deleting characters.
The number of characters to delete.
A new string that is equivalent to this instance except for the removed characters.
Either startIndex
or count
is less than zero.
-or-
startIndex
plus count
specify a position outside this instance.
The following 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 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);
}
}
}
// The example displays the following output:
// The entire name is 'Michelle Violet Banks'
// After removing the middle name, we are left with 'Michelle Banks'
let name = "Michelle Violet Banks"
printfn $"The entire name is '{name}'"
// Remove the middle name, identified by finding the spaces in the name.
let foundS1 = name.IndexOf " "
let foundS2 = name.IndexOf(" ", foundS1 + 1)
if foundS1 <> foundS2 && foundS1 >= 0 then
let name = name.Remove(foundS1 + 1, foundS2 - foundS1)
printfn $"After removing the middle name, we are left with '{name}'"
// The example displays the following output:
// The entire name is 'Michelle Violet Banks'
// After removing the middle name, we are left with 'Michelle Banks'
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
End Class
' The example displays the following output:
' The entire name is 'Michelle Violet Banks'
' After removing the middle name, we are left with 'Michelle Banks'
In the .NET Framework, strings are zero-based. The value of the startIndex
parameter can range from zero to one less than the length of the string instance.
Note
This method does not modify the value of the current instance. Instead, it returns a new string in which the number of characters specified by the count
parameter have been removed. The characters are removed at the position specified by startIndex
.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.
public:
System::String ^ Remove(int startIndex);
public string Remove(int startIndex);
member this.Remove : int -> string
Public Function Remove (startIndex As Integer) As String
The zero-based position to begin deleting characters.
A new string that is equivalent to this string except for the removed characters.
startIndex
is less than zero.
-or-
startIndex
is greater than the length of this instance.
The following example demonstrates the Remove method. The next-to-last case removes all text starting from the specified index through the end of the string. The last case removes three characters starting from the specified index.
// This example demonstrates the String.Remove() method.
using System;
class Sample
{
public static void Main()
{
string s = "abc---def";
Console.WriteLine("Index: 012345678");
Console.WriteLine("1) {0}", s);
Console.WriteLine("2) {0}", s.Remove(3));
Console.WriteLine("3) {0}", s.Remove(3, 3));
}
}
/*
This example produces the following results:
Index: 012345678
1) abc---def
2) abc
3) abcdef
*/
// This example demonstrates the String.Remove() method.
let s = "abc---def"
printfn "Index: 012345678"
printfn $"1) {s}"
printfn $"2) {s.Remove 3}"
printfn $"3) {s.Remove(3, 3)}"
(*
This example produces the following results:
Index: 012345678
1) abc---def
2) abc
3) abcdef
*)
' This example demonstrates the String.Remove() method.
Class Sample
Public Shared Sub Main()
Dim s As String = "abc---def"
'
Console.WriteLine("Index: 012345678")
Console.WriteLine("1) {0}", s)
Console.WriteLine("2) {0}", s.Remove(3))
Console.WriteLine("3) {0}", s.Remove(3, 3))
End Sub
End Class
'
'This example produces the following results:
'
'Index: 012345678
'1) abc---def
'2) abc
'3) abcdef
'
In the .NET Framework, strings are zero-based. The value of the startIndex
parameter can range from zero to the length of the string instance.
Note
This method does not modify the value of the current instance. Instead, it returns a new string in which all characters from position startIndex
to the end of the original string have been removed.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in