Skip to content

MID(函数)

来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgMidfunction 最后更新: 2025-04-05

从字符串中返回子字符串。

语法

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

用法:

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

参数

  • str — 源字符串。
  • start — 在 str 中的起始位置。第一个字符的位置为 1。
  • n — 子字符串的长度(字符数)。

说明

返回从 strstart 位置开始的子字符串。

  • str 为空,返回 ""
  • start <= 0start > Len(str),返回 ""
  • 在第一种形式(不带 n)中:返回从 start 开始的所有剩余字符。
  • 在第二种形式中:若 n < 0n > Len(str) - start,返回所有剩余字符。

示例

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

输出:

cd
cdefg
b

Unicode 示例:

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

平台差异

  • DOS 不支持 Mid 的宽字符字符串版本。

方言差异

  • -lang qb 方言中,字符串类型后缀 $ 是必须的。
  • -lang fblite 方言中,字符串类型后缀 $ 是可选的。
  • -lang fb 方言中,字符串类型后缀 $ 被忽略。

与 QB 的差异

  • QB 不支持 Unicode。

另请参阅

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系