Mid$

sResult = Mid$ ( sSource AS String , iStart AS Integer [ , iLength AS Integer ] ) AS String sResult = Mid ( sSource AS String , iStart AS Integer [ , iLength AS Integer ] ) AS String

Returns a String, which is that part of the source String sSource, which starts as position iStart and has the length iLength.

If iLength is not specified, everything from the position iStart to the end is returned.

If iLength is negative, everything from the position Start except the (- iLength ) last characters is returned.

Mid$ is optimized so that no string duplication occurs to generate the result.

This function only deals with ASCII strings. To manipulate UTF-8 strings, use the String.Mid class.

Using string like arrays (as known as "Boxed strings")

Seit 3.12

You can get the contents of a string using the following array-like syntax:

Result = String [ Start As Integer [ , Length As Integer ] ]

In that case, the character positions start at index zero, not one!

Examples

PRINT Mid$("Gambas", 3, 2)
mb

PRINT Mid$("Gambas", 4)
bas

PRINT Mid$("Gambas", 2, -1)
amba

Dim sStr As String = "Gambas"
Print sStr[0];;sStr[3, 2];; sStr[2, 2];; sStr[2, -1]
G ba mb mba

Mid$ (assignment)

Mid$ ( Variable AS String , Start AS Integer [ , Length AS Integer ] ) = Expression Mid ( Variable AS String , Start AS Integer [ , Length AS Integer ] ) = Expression

This syntax allows to modify the contents of a string variable.

It is just syntactic sugar, i.e. the compiler internally replaces:

Mid$(Variable, Start, Length) = Expression

By the following code:

Variable = Left$(Variable, Start - 1) & Expression & Mid$(Variable, Start + Length)

If the third argument is not specified, i.e. if you use:
Mid$(Variable, Start) = Expression
Then the generated code will be equivalent to:
Variable = Left$(Variable, Start - 1) & Expression
In other words, everything from Start to the end of the string is replaced!

See also