VB.NET Substring Examples

Use the Substring Function to get parts of strings. Review the arguments to Substring.
Substring. This function gets parts of a String. It receives 2 arguments. These indicate the start index, and the length, of the character data.
A new String. Substring() returns a new String instance containing the range of characters. It throws Exceptions on invalid arguments.
First example. Here we use Substring to get the first several characters of an input string. It takes the first 3 chars from the source string and copies them into a new string.

Result: A new String is returned. We can manipulate this substring more easily in other program parts.

Arguments: Substring() uses arguments for the start index and the length of the string. These are both integers.

So: The arguments of 0 and 3 indicate a substring starting at the zero index and continues for three characters.

VB.NET program that uses Substring Module Module1 Sub Main() Use this string literal for the demonstration. Dim literal As String = "CatDogFence" ' Take the first three characters into a new string. ' ... Use the Substring method. Dim substring As String = literal.Substring(0, 3) ' Write the results to the screen. Console.WriteLine("Substring: {0}", substring) End Sub End Module Output Substring: Cat
One argument. Next we call the Substring Function with a single argument. Substring will internally copy all the string data at that index and following that index.

Tip: When we use Substring with one argument, the return value contains all the character data starting at, and following, that index.

VB.NET program that uses Substring with one argument Module Module1 Sub Main() ' Use this string literal. ' ... Next, use the Substring method with one parameter. Dim literal As String = "CatDogFence" Dim substring As String = literal.Substring(6) Console.WriteLine("Substring: {0}", substring) End Sub End Module Output Substring: Fence
Middle characters. We can take the middle chars in a string. The Substring method can be used with a start index and a length that is greater than zero to acquire the middle characters.

Note: The example provides a clear demonstration of taking characters number four through seven.

VB.NET program that uses Substring for middle characters Module Module1 Sub Main() ' Use the string literal. ' ... Then use Substring to take a middle substring. Dim literal As String = "CatDogFence" Dim substring As String = literal.Substring(3, 3) Console.WriteLine("Result: {0}", substring) End Sub End Module Output Result: Dog
Relative indexes. We can base the arguments to Substring on the result of the Length property of the original string minus some constant.

Tip: This allows some functions to handle more input strings, reducing the amount of special-casing you must do.

Tip 2: This is a way you can base the length of the Substring on the length of the original string, providing more flexible code.

VB.NET program that uses Substring with Length arguments Module Module1 Sub Main() ' Use Length to handle relative indexes. Dim literal As String = "CatDogFence" Dim substring As String = literal.Substring(3, literal.Length - 5 - 3) Console.WriteLine("Middle string: {0}", substring) End Sub End Module Output Middle string: Dog
One Char. We can get a one-char string with the Substring function. But for this special case, we can just access a Char from the String, which is much faster. No allocation is needed.
VB.NET program that uses one-char substrings Module Module1 Sub Main() Dim value As String = "CAT" ' Get a one-char substring as a Char. ' ... This is faster. Dim middle1 As Char = value(1) Console.WriteLine(middle1) ' Get a one-char substring as a String. Dim middle2 As String = value.Substring(1, 1) Console.WriteLine(middle2) End Sub End Module Output A A
First words. To get the first words from a String, we can count word separators (like spaces) in a For-loop. Then we take a Substring to get just those words.

Here: The FirstWords method counts spaces in the input string. Once the required number is reached, a Substring is returned.

Words: The correct number of words are present in the output. There are 5 and 3 words in the result strings.

VB.NET program that gets first words from string Module Module1 Public Function FirstWords(input As String, count As Integer) As String Dim words = count For i As Integer = 0 To input.Length - 1 ' Decrement word count when we reach a space. If input(i) = " " Then words -= 1 End If ' When no words remaining, return a substring to this point. If words = 0 Then Return input.Substring(0, i) End If Next Return "" End Function Sub Main() ' Test our FirstWords method. Dim example1 = "This is an example summary that we are using." Console.WriteLine(FirstWords(example1, 5)) Dim example2 = "How many words are in this sentence?" Console.WriteLine(FirstWords(example2, 3)) End Sub End Module Output This is an example summary How many words
Right. In some programs a Function that calls Substring in a special way may be helpful. Here we see a Right method. It returns a substring located on the right side of a string.

Arguments: The first argument is the string to take the substring from. And the second is the length of the substring part.

VB.NET program that uses substring in Right method Module Module1 Function Right(value As String, length As Integer) As String ' Get rightmost characters of specified length. Return value.Substring(value.Length - length) End Function Sub Main() ' Test the Right function. Dim phrase1 As String = "cat and dog" Dim result1 As String = Right(phrase1, 3) Console.WriteLine(result1) End Sub End Module Output dog
String copying. Let us consider Substring's internal implementation. When we call the Substring function, a new string copy is made. A reference to this string data is returned.

Note: When we assign to the result of the Substring function, we are performing a bitwise copy of only that reference.

Also: In some algorithms, using strings that already exist is much faster than creating substrings.

Mid. VB.NET has special support for the Mid built-in statement. With Mid, we can get a substring or assign to a substring based on start and length arguments.Mid Statement
Between substrings. Sometimes a simple parser may need to find substrings between, before and after other ones. Some functions can be used to implement this useful logic.Between, Before, After
This function. Substring is a simple way to copy a series of characters from a source string into a new string that is allocated upon the managed heap.
Argument review. Substring is called with one or two arguments, both of type Integer. These arguments designate the start index, and the length, of our substring.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls