Mid, Mid$, MidB, MidB$ (functions)

Syntax

Mid[$](string, start [,length])

MidB[$](string, start [,length])

Description

Returns a sub-string of the specified string, beginning with start, for length characters (for Mid and Mid$) or bytes (for MidB and MidB$).

Comments

The functions start and length are:

 

Functions

Start and Length of Return

 

Mid and Mid$

Sub-string starting at character position start and will be length characters long

 

MidB and MidB$

Sub-string starting at byte position start and will be length bytes long.

 

The functions return the following.

 

Functions

Return

 

Mid$ and MidB

String

 

Mid and MidB

String variant

 

The returned sub-string starts at character position start and will be length characters long.

Mid$ returns a String, whereas Mid returns a String variant.

The Mid/Mid$ functions take the following parameters:

 

Parameter

Description

 

string

Any String expression containing the text from which data is returned.

 

start

Integer specifying the position where the sub-string begins. If start is greater than the length of string, then a zero-length string is returned.

 

length

Integer specifying the number of characters or bytes to return. If this parameter is omitted, then the entire string is returned, starting at start.

 

The Mid function will return Null text is Null.

The MidB and MidB$ functions are used to return a sub-string of bytes from a string containing byte data.

Example

'This example displays a substring from the middle of a

'string variable using the Mid$ function and replaces the

'first four characters with "NEW " using the Mid$ statement.

Const crlf = Chr$(13) + Chr$(10)

Sub Main()

a$ = "This is the Main string containing text."

b$ = Mid$(a$,13,Len(a$))

Mid$ (b$,1) = NEW "

MsgBox a$ & crlf & b$

End Sub

See Also

InStr, InStrB (functions), Option Compare (statement), Mid, Mid$, MidB, MidB$ (statements)

More information

M