.NET Array Dictionary List String Sub ArrayList Cast Class Console Dates DataTable DateTime Enum File For Format If IndexOf Lambda LINQ Nothing Parse Process Property Regex Replace Select Sort Split StringBuilder Substring

VB.NET
Substring method example

Substring. This function gets parts of a String. It receives two arguments. These indicate the start index, and the length, of the character data.


Constructor: create new

A new String. Substring() returns a new String instance containing the range of characters. It throws Exceptions on invalid arguments.


Method

First example. Here we use Substring to get the first several characters of an input string. It takes the first three 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 zero and three indicate a substring starting at the zero index and continues for three characters.

Based on:

.NET 4.5

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: 1

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 you 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
Letters of the alphabet: ABC

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
Index

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
Char-type

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
Performance optimization

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 you assign to the result of the Substring function, you are performing a bitwise copy of only that reference.

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


Go copyright

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.


The VB.NET programming language

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.