O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Retrieve a Portion of a String

Problem

You need to retrieve a portion of a string based on its position and length.

Solution

Use the String.Substring method.

Discussion

The String.Substring method requires two integer parameters, a startIndex and a length. As with all string indexes, startIndex is zero-based (in other words, the first character in the string is designated as character 0).

Dim FullName As String = "Bill Jones"
Dim FirstName As String

' Retrieve the 4-character substring starting at 0.
FirstName = FullName.Substring(0, 4)
' FirstName is now "Bill"

Optionally, you can omit the length parameter to take a substring that continues to the end of the string:

Dim FullName As String = "Bill Jones" Dim LastName As String ' Retrieve the substring ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required