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
strcontainsNothing, Mid returnsNothing.If
startis greater than the length ofstr, a zero-length string is returned.If
startis less than zero, runtime error 5, “Invalid procedure call or argument,” is generated.If
lengthis omitted orlengthis greater than the length ofstr, all characters fromstartto the end ofstrare 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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access