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
.
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.
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.
Remove
with one argument. This indicates where the string
will be stopped.SetLength
function on the String
type would work.Length
of the String
will always be equal to the argument passed to Remove
(if valid).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 ModuleREMOVE RESULT: bird frog
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
.
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 ModuleRESULT: ade
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.
Substring
function.Remove()
function on the StringBuilder
type. This avoids string
copies, and is sometimes faster.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.