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 — 子字符串的长度(字符数)。
说明
返回从 str 中 start 位置开始的子字符串。
- 若
str为空,返回""。 - 若
start <= 0或start > Len(str),返回""。 - 在第一种形式(不带
n)中:返回从start开始的所有剩余字符。 - 在第二种形式中:若
n < 0或n > Len(str) - start,返回所有剩余字符。
示例
vb
Print Mid("abcdefg", 3, 2)
Print Mid("abcdefg", 3)
Print Mid("abcdefg", 2, 1)输出:
cd
cdefg
bUnicode 示例:
vb
Dim text As WString * 20
text = "Привет, мир!"
Print Mid(text, 6, 4) ' displays "т, м"平台差异
- DOS 不支持
Mid的宽字符字符串版本。
方言差异
- 在
-lang qb方言中,字符串类型后缀$是必须的。 - 在
-lang fblite方言中,字符串类型后缀$是可选的。 - 在
-lang fb方言中,字符串类型后缀$被忽略。
与 QB 的差异
- QB 不支持 Unicode。