VB.NET Remove StringUse the Remove Function on the String type to eliminate characters at an index.
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.
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.
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.
Length The Length of the String will always be equal to the argument passed to Remove (if valid).
String Length
VB.NET program that uses Remove function
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.
VB.NET program that uses Remove, range
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
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.
StringBuilder There is a Remove() function on the StringBuilder type. This avoids string copies, and is sometimes faster.
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.
© 2007-2021 sam allen.
see site info on the changelog.