vb中好何计算字母的个数??

来源:百度知道 编辑:UC知道 时间:2024/06/16 15:22:17
在文本框中输入welcome to beijing ! welcome to china!
分别计算字母c,n,t 的个数,请问怎么编??

private sub countnum()
dim t as string,c1,n1,t1,i as integer
t=text1.text
for i =1 to len(t)
select case mid(t,i,1)
case "c"
c1=c1+1
case "n"
n1=n1+1
case "t"
t1=t1+1
end select
next i
msgbox c1 &","& n1 &","& c1
end sub

给你写一个函数
Private Function stringnum(s1 As String, s2 As String) As Integer
'判断字符串s1在字符串s2中出现的次数
Dim i As Integer
Dim n As Integer
If InStr(s1, s2) = 0 Then
stringnum = 0
Else
n = InStr(s1, s2)
Do
i = i + 1
n = InStr(n + 1, s1, s2)
Loop Until n = 0
stringnum = i
End If
End Function

如c在welcome to beijing ! welcome to china中的出现的次数cnt,则调用
cnt=stringnum("c","welcome to beijing ! welcome to china")

我提供一种另外的方法
Dim strs As String
Dim strf As String
Dim str() As String
strf = "c" &