O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

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

Name

Mid Function

Class

Microsoft.VisualBasic.Strings

Syntax

Mid(str, start[, length])
str (required; String)

The expression from which to return a substring

start (required; Long)

The starting position of the substring

length (optional; Long)

The length of the substring

Return Value

String

Description

Returns a substring of a specified length from a given string

Rules at a Glance

  • If str contains Nothing, Mid returns Nothing.

  • If start is greater than the length of str, a zero-length string is returned.

  • If start is less than zero, runtime error 5, “Invalid procedure call or argument,” is generated.

  • If length is omitted or length is greater than the length of str, all characters from start to the end of str are returned.

Example

The following example parses the contents of a text box control (named txtString) and writes each word to a list box (named lstWord). Note the use of the InStr function to determine the position of either a space or a carriage return/line feed character combination — the two characters that can terminate a word in this case:

Private Sub btnParse_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnParse.Click Dim strString, strWord As String Dim intStart, intEnd, intStrLen, intCrLf As Integer Dim blnLines As Boolean lstWords.Items.Clear( ) intStart = 1 strString = Trim(txtString.Text) intStrLen = Len(strString) intCrLf = InStr(1, strString, vbCrLf) If intCrLf Then blnLines = True lstWords.BeginUpdate( ) Do While intStart > 0 intEnd = InStr(intStart, ...

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