This documentation is archived and is not being maintained.

Mid Function

Returns a string containing a specified number of characters from a string.

Public Shared Function Mid( _
   ByVal Str As String, _
   ByVal Start As Integer, _
   Optional ByVal Length As Integer _
) As String

Parameters

Str
Required. String expression from which characters are returned.
Start
Required. Integer expression. Character position in Str at which the part to be taken starts. If Start is greater than the number of characters in Str, the Mid function returns a zero-length string (""). Start is one based.
Length
Optional. Integer expression. Number of characters to return. If omitted or if there are fewer than Length characters in the text (including the character at position Start), all characters from the start position to the end of the string are returned.

Exceptions/Errors

Exception type Error number Condition
ArgumentException 5 Start <= 0 or Length < 0.

Remarks

To determine the number of characters in Str, use the Len function.

Note   The MidB function in previous versions of Visual Basic returns a string in bytes rather than characters. It is used primarily for converting strings in double-byte character set (DBCS) applications. All Visual Basic .NET strings are in Unicode, and MidB is no longer supported.

Example

This example uses the Mid function to return a specified number of characters from a string.

Dim MyString, FirstWord, LastWord, MidWords As String
MyString = "Mid Function Demo"   ' Creates text string.
FirstWord = Mid(MyString, 1, 3)   ' Returns "Mid".
LastWord = Mid(MyString, 14, 4)   ' Returns "Demo".
MidWords = Mid(MyString, 5)   ' Returns "Function Demo".

Requirements

Namespace: Microsoft.VisualBasic

Module: Strings

Assembly: Microsoft Visual Basic .NET Runtime (in Microsoft.VisualBasic.dll)

See Also

Strings | String Manipulation | Left Function | Len Function | LTrim, RTrim, and Trim Functions | Mid Statement | Right Function | ArgumentException Class | Programming Element Support Changes Summary

Show: