首页 > 开发 > 综合 > 正文

用函数简化你的字符串连接语句

2024-07-21 02:12:32
字体:
来源:转载
供稿:网友
国内最大的酷站演示中心!

'2004/07/27 朱彦志(goodidea) [email protected]

'你是否非常讨厌 用 & 一大串的连接变量?
'你是否不得不经常使用""与"""" ?
'就好像这样:
'strfilter = strfilter & " and [" & strfldname & "] like """ & strvalue & """"
'
'strsql ="insert into tblpubstyle (class,prop,value,type,[set],memo,flag) values ('" & _
' ctl.controltype & "','" & _
' prp.name & "','" & _
' prp.value & "','" & _
' prp.type & "', " & _
' 2 & "," & _
' "' ', " & _
' false ")"
'
'strcnn = "provider=sqloledb;" & _
' "data source=" & strservername & ";" & _
' "initial catalog=" & strdbname & ";" & _
' "user id=" & struid & ";" & _
' "password=" & strpwd & ";"
'
'用它,gcombinationstring,这个十分简单的函数,让冗长的代码更简洁,增强语句可读性
'以下是它的使用典型形式:
'strfilter = gcombinationstring( "$1 and [$2] like ""$3""",strfilter,strfldname,strvalue)
'strcnn= gcombinationstring("provider=sqloledb;data source=$1;initial catalog=$2;user id=$3;password=$4;",_
' strservername ,strdbname ,struid, strpwd)
'参数个数不限,参数类型不限
' strsql = gcombinationstring("insert into tblpubstyle (class,prop,value,type,[set],memo,flag) " & _
' " values ('$1','$2','$3','$4',$5,$6)", _
' ctl.controltype, prp.name, prp.value, prp.type, 2, false)
'

const strcharpre = "$"
dim varitem as variant
dim i as integer

i = 0
for each varitem in para
i = i + 1
strtext = vba.replace(strtext, strcharpre & i, varitem)
strtext = vba.replace(strtext, strcharpre & "/", strcharpre)
next
gcombinationstring = strtext
end function

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表