Get Substring Method
The Get Substring method returns a portion of a string, starting at a location in the string that you specify. It allows you to get a substring from a string. It accepts any type of string, including numeric values, and converts the input value to a string. To modify a portion of a string, see Replace String Method.
Format
Mid[$](string, start[, length])
For information about the dollar sign, see Usage of the Dollar Sign.
The following table describes the arguments that you can use with this method.
Argument | Description |
---|---|
string |
A string or string expression that contains the string that this method copies. If this value is NULL, then this method returns a Null variant. Mid$ requires the string argument to be of type string or variant. For more information, see Variants. |
start |
An integer that identifies the starting position in the string argument to begin copying characters. The position of the first character in a string is 1. If the number in the start argument is larger than the number of positions that the string contains, then this method returns a null string (""). |
length |
An integer that specifies the number of characters to copy. |
Example
The following example returns the last name in a string that the user enters:
Sub Button_Click
Dim username as String
Dim position as Integer
username = "Chris Smith"
Do
position = InStr(username," ")
If position = 0 then
Exit Do
End If
position = position + 1
username = Mid(username,position)
Loop
End Sub