Home
Map
String RemoveUse the Remove Function on the String type to eliminate characters at an index.
VB.NET
This page was last reviewed on Nov 9, 2021.
Remove. This VB.NET function, part of the String class, makes a String shorter. It eliminates all characters starting at a specified position in the String.
Notes, Remove. With the Remove Function, we can take off any number of characters starting at an index. It is a special form of the Substring() Function.
String Substring
String Insert
First example. We declare a String with some characters on the end we want to remove. Then with LastIndexOf, we find the index of the last space character.
String LastIndexOf
Here We call Remove with one argument. This indicates where the string will be stopped.
Tip All characters following that position are eliminated. This is how a SetLength function on the String type would work.
Detail The Length of the String will always be equal to the argument passed to Remove (if valid).
String Length
Module Module1 Sub Main() Dim value As String = "bird frog dog" ' Find position of last space. Dim index As Integer = value.LastIndexOf(" "c) ' Remove everything starting at that position. value = value.Remove(index) Console.WriteLine("REMOVE RESULT: {0}", value) End Sub End Module
REMOVE RESULT: bird frog
Range example. We can specify a count. Only the number of characters specified as the count are removed. Characters following this range are kept in the String.
Argument 1 This is an Integer that indicates the starting index of the range of characters we want to remove.
Argument 2 This is the count of chars (part of the range) we want to remove. It is not an end index.
Module Module1 Sub Main() Dim value As String = "abcde" ' Remove 2 chars starting at index 1. Dim removed As String = value.Remove(1, 2) Console.WriteLine("RESULT: {0}", removed) End Sub End Module
RESULT: ade
Implementation. Remove() with 1 argument calls into Substring() with an argument of 0. Substring specifies the character range to keep, while Remove specifies the range to discard.
Tip Because of this implementation, it is somewhat faster to directly call the Substring function.
Detail There is a Remove() function on the StringBuilder type. This avoids string copies, and is sometimes faster.
StringBuilder
A summary. We used the Remove function. While Substring specifies the characters we want to keep in a new string, Remove specifies the characters we want to discard.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 9, 2021 (image).
Home
Changes
© 2007-2024 Sam Allen.