Skip to content

MID (Function)

Source: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgMidfunction Last revised: 2025-04-05

Returns a substring from a string.

Syntax

declare function Mid ( byref str as const string, byval start as integer ) as string
declare function Mid ( byval str as const wstring ptr, byval start as integer ) as wstring
declare function Mid ( byref str as const string, byval start as integer, byval n as integer ) as string
declare function Mid ( byval str as const wstring ptr, byval start as integer, byval n as integer ) as wstring

Usage:

result = Mid[$]( str, start [, n ] )

Parameters

  • str — The source string.
  • start — The start position in str. The first character starts at position 1.
  • n — The substring length, in characters.

Description

Returns a substring starting from start in str.

  • If str is empty, returns "".
  • If start <= 0 or start > Len(str), returns "".
  • In the first form (without n): returns all remaining characters from start.
  • In the second form: if n < 0 or n > Len(str) - start, returns all remaining characters.

Examples

vb
Print Mid("abcdefg", 3, 2)
Print Mid("abcdefg", 3)
Print Mid("abcdefg", 2, 1)

Output:

cd
cdefg
b

Unicode example:

vb
Dim text As WString * 20
text = "Привет, мир!"
Print Mid(text, 6, 4)  ' displays "т, м"

Platform Differences

  • DOS does not support the wide-character string versions of Mid.

Dialect Differences

  • The string type suffix $ is required in the -lang qb dialect.
  • The string type suffix $ is optional in the -lang fblite dialect.
  • The string type suffix $ is ignored in the -lang fb dialect.

Differences from QB

  • QB does not support Unicode.

See Also

Translated from FreeBASIC official docs. Contact us for removal if infringed.
FreeBASIC is an open-source project, not affiliated with Microsoft