PowerBuilder 高频实用函数清单:入门必会的 50 个 PowerScript 函数
PowerBuilder 高频实用函数清单:入门必会的 50 个 PowerScript 函数阅读说明
① 适用版本:PowerBuilder 9.0 ~ 12.5(PBIDEA 亦兼容,均为内置全局函数);
② 支持数据库:不依赖任何数据库,纯语言层面;
③ 操作系统与环境要求:Windows 7 及以上,无需额外组件;
④ 难度系数:★★☆☆☆(入门);
⑤ 其它阅读说明:适合刚接触 PowerScript 的读者按场景速查,示例可直接粘贴到按钮 Click 事件运行。
为什么需要一张函数清单
PowerScript 内置了 200 多个全局函数,但日常开发里 80% 的代码只用其中几十个。与其翻帮助,不如按场景记一张高频清单:字符串、类型转换、数值、日期时间、数组、文件、杂项。本文把这七类讲清楚,每个函数都配一句话说明和一个最小示例。
1. 字符串处理:最高频的一组
函数作用示例
Len()取字符个数Len("abc") 返回 3
Trim()去掉两端空格Trim(" ab ") 返回 "ab"
Left() / Right()取左/右 N 个字符Left("abcd", 2) 返回 "ab"
Mid()从第 N 位取 M 个字符Mid("abcd", 2, 2) 返回 "bc"
Pos()找子串位置,找不到返回 0Pos("abcd", "bc") 返回 2
Upper() / Lower()转大写 / 小写Upper("ab") 返回 "AB"
Replace()替换子串Replace("abcd", 1, 2, "XY") 返回 "XYcd"
Fill() / Space()生成重复字符 / 空格Fill("-", 3) 返回 "---"
Reverse()反转字符串Reverse("abc") 返回 "cba"
NullToEmpty()NULL 转空串,防报错NullToEmpty(ls_x)
// 输入值
string ls_name = "PowerBuilder"
string ls_word = "hello world"
// 长度与去空格
long ll_len
ll_len = Len(ls_name) // 去空格前长度
string ls_trim
ls_trim = Trim(ls_name) // 去掉两端空格
// 截取
string ls_left
ls_left = Left(ls_word, 5) // "hello"
string ls_mid
ls_mid = Mid(ls_word, 7, 5) // "world"
string ls_right
ls_right = Right(ls_word, 5) // "world"
// 查找与替换
long ll_pos
ll_pos = Pos(ls_word, "world") // 7
string ls_rep
ls_rep = Replace(ls_word, 1, 5, "Hi") // "Hi world"
// 大小写与填充
string ls_up
ls_up = Upper(ls_word) // "HELLO WORLD"
string ls_fill
ls_fill = Fill("-", 10) // "----------"
// 空值兜底:变量没赋值时是 NULL,直接拼串会整段变 NULL
string ls_maybe
string ls_safe
ls_safe = NullToEmpty(ls_maybe) // ""
MessageBox("字符串示例", ls_trim + " / " + ls_left + " / " + ls_mid + " / " + ls_fill)
2. 类型转换:字符串和数值互转
函数作用示例
Long() / Integer() / Double() / Decimal()字符串转数值Long("32") 返回 32
String()数值转字符串,可带格式String(3.14159, "#.00") 返回 "3.14"
Val()从头解析数字,遇非数字停止Val("12ab") 返回 12
IsNumber()判断字符串是否为数字IsNumber("3.14") 返回 TRUE
Date() / DateTime() / Time()各类型互转Date("2026-08-21")
// 输入值
string ls_num = "3.14159"
string ls_age = "32"
// 字符串转数值
long ll_age
ll_age = Long(ls_age) // 32
double ld_pi
ld_pi = Double(ls_num) // 3.14159
// 转换前先判断,避免运行时出错
boolean lb_ok
lb_ok = IsNumber(ls_num) // TRUE
if not lb_ok then
MessageBox("转换示例", "不是数字")
return
end if
// 数值转字符串,带格式
string ls_pi
ls_pi = String(ld_pi) // "3.14159"
string ls_fmt
ls_fmt = String(ld_pi, "#.00") // "3.14"
MessageBox("转换示例", ls_pi + " / " + ls_fmt + " / 年龄=" + String(ll_age))
3. 数值计算:取整、绝对值、最值
函数作用示例
Abs()绝对值Abs(-3.7) 返回 3.7
Ceiling() / Floor()向上 / 向下取整Ceiling(2.5) 返回 3
Truncate()向零截断小数Truncate(-3.7) 返回 -3
Round()四舍五入Round(2.56, 1) 返回 2.6
Max() / Min()取大 / 取小Max(2, 5) 返回 5
Mod()求余数Mod(17, 5) 返回 2
Rand()返回 0~1 随机数Rand() 返回 0~1 之间
// 输入值
double ld_x = -3.7
double ld_y = 2.5
// 取整
integer li_ceil
li_ceil = Ceiling(ld_y) // 3
integer li_floor
li_floor = Floor(ld_y) // 2
integer li_trunc
li_trunc = Truncate(ld_x) // -3(向零截断,注意与 Floor 区别)
// 绝对值与四舍五入
double ld_abs
ld_abs = Abs(ld_x) // 3.7
double ld_round
ld_round = Round(ld_y, 0) // 3
// 最值与余数
double ld_max
ld_max = Max(ld_x, ld_y) // 2.5
double ld_min
ld_min = Min(ld_x, ld_y) // -3.7
integer li_mod
li_mod = Mod(17, 5) // 2
MessageBox("数值示例", String(li_ceil) + "/" + String(li_floor) + "/" + String(li_trunc) + "/ 余数=" + String(li_mod))
4. 日期时间:Today 与 Now 是起点
函数作用示例
Today()今天日期Today() 返回 2026-08-21
Now()当前日期时间Now()
Year() / Month() / Day()拆出年 / 月 / 日Year(Today()) 返回 2026
Hour() / Minute() / Second()拆出时 / 分 / 秒Hour(Now())
DayName() / DayNumber()星期几(名 / 数)DayName(Today()) 返回 "星期五"
DaysAfter()两个日期相差天数DaysAfter(今天, 明天) 返回 1
RelativeDate() / RelativeTime()日期 / 时间加减RelativeDate(Today(), 30)
SecondsAfter()两个时间相差秒数SecondsAfter(t1, t2)
// 输入值(以下两句是取值,不需要先声明再赋值之外的特别处理)
date ld_today
ld_today = Today() // 今天
datetime ldt_now
ldt_now = Now() // 当前日期时间
// 拆分
integer li_year
li_year = Year(ld_today) // 2026
integer li_month
li_month = Month(ld_today) // 8
integer li_day
li_day = Day(ld_today) // 21
// 日期计算
long ll_days
ll_days = DaysAfter(ld_today, Date(2026, 9, 1)) // 距 9 月 1 日还有几天
date ld_future
ld_future = RelativeDate(ld_today, 30) // 30 天后是哪天
// 星期几
string ls_week
ls_week = DayName(ld_today) // 星期五
MessageBox("日期示例", String(li_year) + "-" + String(li_month) + "-" + String(li_day) + " " + ls_week + " / 30天后=" + String(ld_future))
5. 数组:上下界与遍历
函数作用示例
LowerBound()数组下界LowerBound(ls_arr) 通常返回 1
UpperBound()数组上界(元素个数)UpperBound(ls_arr)
SetNull()把变量置为 NULLSetNull(ls_x)
IsNull()判断是否为 NULLIsNull(ls_x)
// 输入值
string ls_city
ls_city = "北京"
ls_city = "上海"
ls_city = "广州"
// 上下界
integer li_low
li_low = LowerBound(ls_city) // 1
integer li_high
li_high = UpperBound(ls_city) // 3
// 遍历拼接(循环变量先声明,经典 PB 不支持循环内联声明变量)
integer li_i
string ls_all = ""
for li_i = li_low to li_high
ls_all = ls_all + ls_city + " "
next
// 空值判断:未赋值的变量是 NULL
string ls_x
boolean lb_null
lb_null = IsNull(ls_x) // TRUE
MessageBox("数组示例", "共" + String(li_high) + "个城市:" + ls_all + " / 空值=" + String(lb_null))
6. 文件读写:记牢开-读/写-关三步
函数作用说明
FileExists()文件是否存在返回 boolean
FileLength()文件字节数返回 long
FileOpen()打开文件模式:LineMode!/StreamMode!,Read!/Write!
FileRead() / FileWrite()读一行 / 写一行返回读写的字符数
FileClose()关闭文件用完必须关
FileDelete()删除文件返回 boolean
GetFileOpenName() / GetFileSaveName()弹出选择文件对话框带文件类型过滤
// 输入值
string ls_path = "C:\temp\demo.txt"
string ls_content = "Hello PowerBuilder"
// 写:开 -> 写 -> 关
integer li_fp
li_fp = FileOpen(ls_path, LineMode!, Write!, LockWrite!, Replace!)
if li_fp > 0 then
FileWrite(li_fp, ls_content)
FileClose(li_fp)
end if
// 读:先确认存在再开
boolean lb_exist
lb_exist = FileExists(ls_path)
string ls_read = ""
integer li_fr
li_fr = FileOpen(ls_path, LineMode!, Read!, LockRead!)
if li_fr > 0 then
FileRead(li_fr, ls_read)
FileClose(li_fr)
end if
// 文件大小
long ll_size
ll_size = FileLength(ls_path)
MessageBox("文件示例", "存在=" + String(lb_exist) + " 大小=" + String(ll_size) + " 内容=" + ls_read)
7. 杂项:弹窗、配置、剪贴板
函数作用说明
MessageBox()弹窗提示最常用的调试与提示手段
Beep()响铃提示Beep(1)
ProfileString()读 ini 配置ProfileString(文件, 节, 键, 默认值)
SetProfileString()写 ini 配置返回值 1 表示成功
Clipboard()读写剪贴板不带参读取,带参写入
IsValid()对象是否已创建IsValid(dw_1)
Create() / Destroy()创建 / 销毁对象配合自定义类使用
// 输入值
string ls_ini = "C:\app\config.ini"
// 读写 ini 配置(键不存在时返回默认值)
string ls_server
ls_server = ProfileString(ls_ini, "database", "server", "127.0.0.1")
SetProfileString(ls_ini, "database", "port", "1433")
// 剪贴板:先读旧内容,再写入新内容
string ls_old
ls_old = Clipboard()
Clipboard("复制这段文字")
// 对象有效性
boolean lb_ok
lb_ok = IsValid(dw_1)
MessageBox("杂项示例", "server=" + ls_server + " / 剪贴板旧值=" + ls_old + " / dw_1 有效=" + String(lb_ok))
小结:怎么记这张清单
入门阶段先死记三组:字符串(Len/Trim/Left/Mid/Pos/Replace)、转换(Long/String/IsNumber)、日期(Today/Year/DaysAfter)——这三组覆盖了日常九成需求。文件操作记住"开-读写-关"三步曲,数组记住 LowerBound/UpperBound。剩下的函数用到再查帮助即可,不必一次全背。
几个易错提醒:
[*]未赋值的变量是 NULL 不是空串,拼字符串前先 NullToEmpty();
[*]Pos() 找不到返回 0,不是 -1;
[*]Truncate 与 Floor 方向不同:负数时 Truncate(-3.7)=-3,Floor(-3.7)=-4;
[*]文件用完必须 FileClose,否则句柄泄漏,长时间运行会打不开文件。
页:
[1]