怎么取得字符串b在a中出现的次数啊

guokunh 2004-11-21 09:53:36
比如函数nums(a,b)
nums(aabdasbfd,b)=2
谢谢拉
...全文
211 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
超级大笨狼 2004-11-21
  • 打赏
  • 举报
回复
这个是完整点的,你可以不用类来实现。写到函数里用不了太多代码,5行足够。


<input value="看看superDullWolf的CNWord类引用实例,vbs,改进了一下,可以提供chinese出现的位置了" id="input1" style="width:100%"/>
<br/>
<button onclick="vbs:classAndRegExp">验证</button>
<script language = "vbscript">
sub classAndRegExp()
set wolf = new CNWord
wolf.str = input1.value

msgbox "含有中文数" & wolf.CNcount
msgbox "含有中文块数" & wolf.CNBlockCount
msgbox "第2个中文块是--->" & wolf.CN(2)
msgbox "第1个中文块出现的位置是--->" & wolf.CNIndex(1)
set wolf = nothing
end sub
</script>


<script language="vbscript">
class CNWord

public str

private sub Class_Initialize()
set regEx =new RegExp
regEx.IgnoreCase = True '设置是否区分大小写。
regEx.Global = True '设置全局可用性。
end sub

private regEx


Property get CN(x)
dim Arr()
dim maxBound : maxBound = CNBlockCount
redim Arr(maxBound)
dim i:i=1
dim Match
For each Match in regEx.Execute(str)
Arr(i) = Match
i = i + 1
next
if x<=maxBound then CN = Arr(x)
End Property

Property get CNIndex(x)
dim Arr()
dim maxBound : maxBound = CNBlockCount
redim Arr(maxBound)
dim i:i=1
dim Match
For each Match in regEx.Execute(str)
Arr(i) = Match.FirstIndex + 1
i = i + 1
next
if x<=maxBound then CNIndex = Arr(x)
End Property

Property get CNcount
'返回含有中文的字数
regEx.Pattern = "[\u4e00-\u9fa5]"
CNcount = findCHNnum()
End Property

Property get CNBlockCount
'返回含有中文的块数
regEx.Pattern = "[\u4e00-\u9fa5]+"
CNBlockCount = findCHNnum()
End Property


private function findCHNnum()
findCHNnum = regEx.Execute(str).count
end function




private sub Class_Terminate()
set regEx = nothing
end sub

end class
</script>
超级大笨狼 2004-11-21
  • 打赏
  • 举报
回复
用正则表达式的match对象。
把下面你用不上的代码剔除,正则表达式换成b就可以了。

<script language="vbscript">
class CNWord

public str

private sub Class_Initialize()
set regEx =new RegExp
regEx.IgnoreCase = True '设置是否区分大小写。
regEx.Global = True '设置全局可用性。
end sub

private regEx


Property get CN(x)
dim Arr()
dim maxBound : maxBound = CNBlockCount
redim Arr(maxBound)
dim i:i=1
For each Match in regEx.Execute(str)
Arr(i) = Match
i = i + 1
next
if x<=maxBound then CN = Arr(x)
End Property



Property get CNcount
'返回含有中文的字数
regEx.Pattern = "[\u4e00-\u9fa5]"
CNcount = findCHNnum()
End Property

Property get CNBlockCount
'返回含有中文的块数
regEx.Pattern = "[\u4e00-\u9fa5]+"
CNBlockCount = findCHNnum()
End Property


private function findCHNnum()
findCHNnum = regEx.Execute(str).count
end function




private sub Class_Terminate()
set regEx = nothing
end sub

end class

set wolf = new CNWord
wolf.str ="superDullWolf的CNWord类引用实例,vbs"

msgbox "含有中文数" & wolf.CNcount
msgbox "含有中文块数" & wolf.CNBlockCount
msgbox "第2个中文块是--->" & wolf.CN(2)

set wolf = nothing
</script>

davs 2004-11-21
  • 打赏
  • 举报
回复
ASP的:
Function nums(str1,str2)
dim temp
nums=0
if instr(str1,str2)>0 then
nums=nums+1
temp=right(str1,len(str1)-instr(str1,str2))
nums=nums+nums(temp,str2)
end if
end function

测试通过
leo963258 2004-11-21
  • 打赏
  • 举报
回复
javascript:
function nums(a,b){
var count=0
for(i=0;i<a.length;i++){
if(a.charAt(i)=="b")
count++
}
return count;
}
ygjwjj 2004-11-21
  • 打赏
  • 举报
回复
function nums(str1,str2)
dim count
if instr(str1,str2)>0 then

for i=1 to len(str1)-len(str2)
if instr(i,str1,str2)>0 then
count=count+1
end if
next
end if
nums=count
end function
patchclass 2004-11-21
  • 打赏
  • 举报
回复
用正则可能也行
QQgenie 2004-11-21
  • 打赏
  • 举报
回复
function nums(a,b)
i=0
do while instr(a,b)>0
i=i+1
a=mid(a,instr(a,b)+len(b),len(a))
loop

nums=i
end function

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧