欢迎光临
我们一直在努力

获取闭合符号中的字符串-.NET教程,数据库应用

建站超值云服务器,限时71元/月

在解决iif问题时,为了判断iif语句是否合法,同时找出其中的条件,返回值true及返回值false。却不自然中写出了一个通用的函数模块,该函数能解决闭合符号形式中的一串文字。如”( … )”、”[ … ]”等,看官也可以自己设定一个闭合符号或字符串,但必须是对称出现,如“avb”和“def”便可以组合成一对,这样对于字符串avbcdeokdef的闭合符号中。现将这些函数整理如下,参数、返回值及函数功用都已经在注释中说明,希望本函数模块能对vb忠实爱好者有所帮助。

————————————————————————–

从某一段文字中查找某一个符号(须考虑大小写),并且返回该符号的所有位置索引

douhapy 2005-01-31

参数:

strsentence :任意一段文字

strsymbol :需要查找的特殊符号,或字符串

symbolindex() :返回该符号在文字中的所处位置索引

blcasematch :是否必须大小写匹配 (true 大小写必须匹配)

bldesc :是否降序排列symbolindex中的数据(true 为降序排列索引)

返回值:

true 成功找到该符号,同时symbolindex有相应的值

————————————————————————–

function checksymbolfromsentence(byval strsentence as string, byval strsymbol as string, _

byref symbolindex() as integer, optional byval blcasematch = true, optional byval bldesc = false) as boolean

dim intsymbolindex() as integer

dim strtmp as string

dim inttmp as integer

dim blreturn as boolean

dim i as integer

strtmp = strsentence: blreturn = false: i = 0

if bldesc then

if blcasematch then

inttmp = instrrev(strtmp, strsymbol)

else

inttmp = instrrev(strtmp, strsymbol, -1, vbtextcompare)

end if

else

if blcasematch then

inttmp = instr(strtmp, strsymbol)

else

inttmp = instr(1, strtmp, strsymbol, vbtextcompare)

end if

end if

do while inttmp <> 0

blreturn = true

redim preserve intsymbolindex(i)

intsymbolindex(i) = inttmp

inttmp = inttmp – 1

if inttmp <> 0 then

if bldesc then

if blcasematch then

inttmp = instrrev(strtmp, strsymbol, inttmp)

else

inttmp = instrrev(strtmp, strsymbol, inttmp, vbtextcompare)

end if

else

if blcasematch then

inttmp = instr(inttmp + 1, strtmp, strsymbol)

else

inttmp = instr(inttmp + 1, strtmp, strsymbol, vbtextcompare)

end if

end if

end if

i = i + 1

loop

checksymbolfromsentence = blreturn

symbolindex = intsymbolindex

erase intsymbolindex

end function

————————————————————————–

获取任意一段文字"( … )"闭合符号中的字符串数据

douhapy 2005-01-31

参数:

strsentence :任意一段文字

leftbracketindex:该段文字中闭合符号左符号的索引

leftclosesymbol :闭合符号的左符号

rightclosesymbol:闭合符号的右符号

blcasematch :是否必须大小写匹配 (true 大小写必须匹配)

返回值

若成功 则返回闭合括号中的字符串

否则 返回空字符串

————————————————————————–

function getclosestring(byval strsentence as string, byval leftbracketindex as integer, _

optional byval leftclosesymbol as string = "(", optional byval rightclosesymbol as string = ")", _

optional byval blcasematch as boolean = true) as string

dim strreturn as string

dim strtmp as string

dim intleftbracketindex() as integer 所有左括号的位置

dim intrightbracketindex() as integer 所有右括号的位置

dim i as integer

dim j as integer

dim m as integer

dim mintleftbracketindex as integer

dim mintrightbracketindex as integer

strtmp = strsentence: strreturn = ""

查找第一个左括号

if blcasematch then

mintleftbracketindex = instr(1, strsentence, leftclosesymbol)

else

mintleftbracketindex = instr(1, strsentence, leftclosesymbol, vbtextcompare)

end if

if mintleftbracketindex <> 0 then

if ucase(mid(strsentence, leftbracketindex, len(leftclosesymbol))) = ucase(leftclosesymbol) then

mintleftbracketindex = leftbracketindex

end if

else

goto endlab

end if

获取所有的左括号和右括号的位置

call checksymbolfromsentence(strtmp, leftclosesymbol, intleftbracketindex, blcasematch, true)

call checksymbolfromsentence(strtmp, rightclosesymbol, intrightbracketindex, blcasematch, true)

if ubound(intleftbracketindex) = ubound(intrightbracketindex) then

循环查找匹配的左右对称括号,同时将数据置为0

for i = 0 to ubound(intleftbracketindex)

for j = 0 to ubound(intrightbracketindex)

if intrightbracketindex(j) <> 0 then

if intrightbracketindex(j) < intleftbracketindex(i) then

exit for

end if

if j = ubound(intrightbracketindex) then

j = j + 1: exit for

end if

end if

next

for m = j – 1 to 0 step -1

if intrightbracketindex(m) <> 0 then

if intleftbracketindex(i) = mintleftbracketindex then

mintrightbracketindex = intrightbracketindex(m)

end if

intrightbracketindex(m) = 0

exit for

end if

next

next

strreturn = mid(strtmp, mintleftbracketindex + len(leftclosesymbol), _

mintrightbracketindex – mintleftbracketindex – len(rightclosesymbol))

end if

endlab:

getclosestring = strreturn

erase intleftbracketindex

erase intrightbracketindex

end function

—————————————————————————————————————————————————-

检查iif语句中放在对应"( )"内的语句中的条件表达式、true表达式、false表达式

douhapy 2005-01-31

参数:

strsentence :任意一条语句(该语句含有iif)

strcondition :返回该iif语句中的条件表达式

strreturnt :返回该iif语句中的true表达式

strreturnf :返回该iif语句中的false表达式

返回值:

true 成功查找到所需的表达式

—————————————————————————————————————————————————-

function checkiifsentence(byval strsentence as string, optional byref strcondition as string = "", _

optional byref strreturnt as string = "", optional byref strreturnf as string = "") as boolean

dim strtmp as string

dim striifsentence as string

dim mstrcondition as string iif语句中的条件

dim mstrreturnt as string iif语句中的true结果

dim mstrreturnf as string iif语句中的false结果

dim inttmp1 as integer

dim inttmp2 as integer

dim i as integer

dim j as integer

dim m as integer

dim blsucceed as boolean

dim intleftbracketindex() as integer 所有左括号的位置

dim intrightbracketindex() as integer 所有右括号的位置

————————————————————————–

先查找iif (,判断是否为iif语句

strtmp = replace(strsentence, " ", "")

if instr(1, strtmp, "iif(", vbtextcompare) = 0 then

exit function

end if

————————————————————————–

获取iif中的表达式

strtmp = strsentence

inttmp1 = instr(1, strtmp, "iif", vbtextcompare)

if inttmp1 then

获取离iif最近的左括号的位置,并保存

inttmp1 = instr(inttmp1, strtmp, "(")

striifsentence = getclosestring(strtmp, inttmp1)

blsucceed = true

end if

————————————————————————–

获取iif中的条件以及返回值

if blsucceed then

blsucceed = false

获取条件

inttmp1 = instr(1, striifsentence, ",", vbtextcompare)

if inttmp1 <> 0 then

mstrcondition = mid(striifsentence, 1, inttmp1 – 1)

inttmp2 = instr(inttmp1 + 1, striifsentence, ",", vbtextcompare)

if inttmp2 <> 0 then

获取返回值

mstrreturnt = mid(striifsentence, inttmp1 + 1, inttmp2 – inttmp1 – 1)

mstrreturnf = mid(striifsentence, inttmp2 + 1, len(striifsentence) – inttmp2)

blsucceed = true

end if

end if

end if

checkiifsentence = blsucceed

strcondition = mstrcondition

strreturnt = mstrreturnt

strreturnf = mstrreturnf

end function

private sub command1_click()

dim strtmp as string

dim strcondition as string iif语句中的条件

dim strreturnt as string iif语句中的true结果

dim strreturnf as string iif语句中的false结果

strtmp = "iif (((a+b)-(b+a))- ((b-6)-c) * a ,standout,outtime ) – (standout -outtime /2) > (standout + outtime)"

if checkiifsentence(strtmp, strcondition, strreturnt, strreturnf) then

msgbox "原语句:" & vbcrlf & strtmp & vbcrlf & _

"iif语句中的条件: " & strcondition & vbcrlf & vbcrlf & _

"iif语句中的true返回值: " & strreturnt & vbcrlf & _

"iif语句中的false返回值: " & strreturnf

msgbox getclosestring(strtmp, 57)

end if

strtmp = "{[[a123befgcb[[[["

msgbox getclosestring(strtmp, 4, "{[[a", "[[[[", false)

end sub

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 获取闭合符号中的字符串-.NET教程,数据库应用
分享到: 更多 (0)

相关推荐

  • 暂无文章