VB.NET Substring

Substring method example

Substring gets parts of a String. It receives two arguments. These indicate the start index and the length of the string data. It returns a new String instance containing the range of characters. It throws Exceptions on invalid arguments.

Example

Note

Here we use Substring to get the first several characters of an input string. It takes the first three chars from the source string, copies them into a new string, and then copies the reference to this object on the evaluation stack.

Result:A new string is returned and you can manipulate this substring more easily in other program parts.

Program that uses Substring method: VB.NET

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
Program icon

This program uses a two-argument invocation to the Substring Function. The Substring Function uses these arguments as the start index and the length of the string you want. These are both integer values.

So:The arguments of zero and three indicate you want a substring starting at the zero index and that continues for three characters.

Note:Internally the Substring call creates a new allocation upon the managed heap.

One argument

One: 1

Next we call the Substring Function with a single argument. This overload is used to specify all characters starting at and following that index. Substring will internally copy all the string data at that index and following that index.

Program that uses Substring with one argument: VB.NET

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
Note

This example program shows how the Substring method can be used with only one argument. This argument is the start index value that is used in the internal Substring routine. The count is internally computed.

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

Middle characters

Letters of the alphabet: ABC

You can take the middle characters in a source string and receive a new string with that data. 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.

Program that uses Substring for middle characters: VB.NET

Module Module1
    Sub Main()
	'
	' Use the string literal.
	' ... Then use the Substring method 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

Programming tip

You can apply the concept of relative indexes to the Substring Function. For relative indexes, you 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.

Program that uses Substring with Length arguments: VB.NET

Module Module1
    Sub Main()
	'
	' Use the Length method to use relative indexes into the string buffer.
	'
	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

The Substring Function is invoked with relative indexes. The start index is specified as the constant three, and the length of the substring to be acquired is equal to the original string length minus eight.

IntegerConst

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

String copying

Framework: NET

We consider the internal implementation of taking Substrings and how this affects allocation on the managed heap. When you call the Substring function, a new string copy is made and 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.

Performance optimization

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

Summary

Logo

We looked at the Substring function in the VB.NET language. The Substring function provides a simple way to copy a series of characters from a source string into a new string that is allocated upon the managed heap.

Review:The Substring function can be called with one or two arguments, both of type Integer.

And:These arguments are copied into the formal parameter slots and designate the start index and the length of the substring you require.


VB.NET: String