我有字符串
'TEST1, TEST2, TEST3'
我希望有
'TEST1,TEST2,TEST3'
在powerbuilder中是一个像replace,substr之类的函数吗?
我有字符串
'TEST1, TEST2, TEST3'
我希望有
'TEST1,TEST2,TEST3'
在powerbuilder中是一个像replace,substr之类的函数吗?
当然有(你可以很容易地在帮助中找到它),但它并不是很有帮助。
它的原型是Replace ( string1, start, n, string2 )
,所以在调用它之前你需要知道要替换的字符串的位置。
有一个通用的包装器,它包括循环pos()
/replace()
直到没有任何东西可以替换。下面是一个全局函数的源码:
global type replaceall from function_object
end type
forward prototypes
global function string replaceall (string as_source, string as_pattern, string as_replace)
end prototypes
global function string replaceall (string as_source, string as_pattern, string as_replace);//replace all occurences of as_pattern in as_source by as_replace
string ls_target
long i, j
ls_target=""
i = 1
j = 1
do
i = pos( as_source, as_pattern, j )
if i>0 then
ls_target += mid( as_source, j, i - j )
ls_target += as_replace
j = i + len( as_pattern )
else
ls_target += mid( as_source, j )
end if
loop while i>0
return ls_target
end function
请注意,PB 中的字符串函数(搜索和连接)效率不高,另一种解决方案可能是使用PbniRegex扩展FastReplaceall()
提供的全局函数。它是 PB 经典版 9 到 12 的 c++ 编译插件。
一种方法是使用数据库,因为您可能有一个活动连接。
string ls_stringwithspaces = "String String String String"
string ls_stringwithnospace = ""
string ls_sql = "SELECT replace('" + ls_stringwithspaces + "', ' ', '')"
DECLARE db DYNAMIC CURSOR FOR SQLSA;
PREPARE SQLSA FROM :ls_sql USING SQLCA;
OPEN DYNAMIC db;
IF SQLCA.SQLCode > 0 THEN
// erro handling
END IF
FETCH db INTO :ls_stringwithnospace;
CLOSE db;
MessageBox("", ls_stringwithnospace)
我这样做:
long space, ll_a
FOR ll_a = 1 to len(ls_string)
space = pos(ls_string, " ")
IF space > 0 THEN
ls_string= Replace(ls_string, space, 1, "")
END IF
NEXT