Name
Mid Function
Class
Microsoft.VisualBasic.Strings
Syntax
Mid(str, start[, length]
)
-
str
Use: Required
Data Type: String
The expression from which to return a substring
-
start
Use: Required
Data Type: Long
The starting position of the substring
-
length
Use: Optional
Data Type: 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
containsNothing
,Mid
returnsNothing
.If
start
is greater than the length ofstr
, 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 orlength
is greater than the length ofstr
, all characters fromstart
to the end ofstr
are returned.
Example
The following example parses the contents of a textbox 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 ...
Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.