文章摘要
这篇文章介绍了两个Visual Basic函数的功能及其作用。第一个函数`InterceptString(txt, length)`用于截取字符串,并根据字符性质(中文或ASCII)判断敏感字符串的出现。它通过逐字符检查来确定字符串长度,并在必要时进行截断或延长处理。第二个函数`StrCount(Str, SubStr)`用于统计字符串中特定子字符串的出现次数。文章整体内容围绕字符串处理和字符统计展开,重点描述了两个函数的实现逻辑及其在字符串操作中的应用。
Function InterceptString(txt,length)
Dim x,y,ii,c,ischines,isascii,tempStr
txt=trim(txt)
x=len(txt)
y=0
if x >=1 then
for ii=1 to x
c=asc(mid(txt,ii,1))
if c< 0 or c >255 then
‘说明是一个中文字符
y=y + 2
ischines=1
isascii=0
else
‘说明是一个ascii码
y=y + 1
ischines=0
isascii=1
end if
‘如果长度已经大于定义子字符串长度,就判断是否包含敏感字符串是否分开
if y >=length then
if ischines=1 and StrCount(left(trim(txt),ii),”<a”)=StrCount(left(trim(txt),ii),”</a>”) then
txt=left(trim(txt),ii) ‘”字符串限长
exit for
else
if isascii=1 then x=x+1
end if
end if
next
InterceptString=txt
else
InterceptString=””
end if
End Function
‘判断字符串出现的次数
Function StrCount(Str,SubStr)
Dim iStrCount
Dim iStrStart
Dim iTemp
iStrCount=0
iStrStart=1
iTemp=0
Str=LCase(Str)
SubStr=LCase(SubStr)
Do While iStrStart < Len(Str)
iTemp=Instr(iStrStart,Str,SubStr,vbTextCompare)
If iTemp <=0 Then
iStrStart=Len(Str)
Else
iStrStart=iTemp + Len(SubStr)
Function InterceptString(txt,length)
Dim x,y,ii,c,ischines,isascii,tempStr
txt=trim(txt)
x=len(txt)
y=0
if x >=1 then
for ii=1 to x
c=asc(mid(txt,ii,1))
if c< 0 or c >255 then
‘说明是一个中文字符
y=y + 2
ischines=1
isascii=0
else
‘说明是一个ascii码
y=y + 1
ischines=0
isascii=1
end if
‘如果长度已经大于定义子字符串长度,就判断是否包含敏感字符串是否分开
if y >=length then
if ischines=1 and StrCount(left(trim(txt),ii),”<a”)=StrCount(left(trim(txt),ii),”</a>”) then
txt=left(trim(txt),ii) ‘”字符串限长
exit for
else
if isascii=1 then x=x+1
end if
end if
next
InterceptString=txt
else
InterceptString=””
end if
End Function
‘判断字符串出现的次数
Function StrCount(Str,SubStr)
Dim iStrCount
Dim iStrStart
Dim iTemp
iStrCount=0
iStrStart=1
iTemp=0
Str=LCase(Str)
SubStr=LCase(SubStr)
Do While iStrStart < Len(Str)
iTemp=Instr(iStrStart,Str,SubStr,vbTextCompare)
If iTemp <=0 Then
iStrStart=Len(Str)
Else
iStrStart=iTemp + Len(SubStr)
iStrCount=iStrCount + 1
End If
Loop
StrCount=iStrCount
End Function
© 版权声明
文章版权归作者所有,未经允许请勿转载。