vb统计每个字母出现的次数用什么代码?
答案:5 悬赏:0 手机版
解决时间 2021-03-04 13:12
- 提问者网友:火车头
- 2021-03-03 12:17
vb统计每个字母出现的次数用什么代码?
最佳答案
- 五星知识达人网友:迷人又混蛋
- 2021-03-03 13:48
代码:
运行结果:
运行结果:
全部回答
- 1楼网友:人间朝暮
- 2021-03-03 17:31
Private Sub Form_Load()
Me.AutoRedraw = True
Text1 = "ABCDAABAAD"
Call tjcs
End Sub
Function tjcs()
Dim t As String, j As Long, s As String, a(4) As Long
t = Text1
For j = 1 To Len(t)
s = Mid(t, j, 1)
Select Case s
Case Is = "A"
a(1) = a(1) + 1
Case Is = "B"
a(2) = a(2) + 1
Case Is = "C"
a(3) = a(3) + 1
Case Is = "D"
a(4) = a(4) + 1
End Select
Next
For j = 1 To 4
Print Chr(Asc("A") + j - 1); a(j)
Next
End Function
- 2楼网友:洎扰庸人
- 2021-03-03 17:00
Private Sub Form_Load() Dim file_name As String, zs As Long, zd() As Long, zs1() As Long, strline As String, tp As String
- 3楼网友:独钓一江月
- 2021-03-03 16:11
解:text2为原字符串所在的文本框。
Private Sub Form_Click()
Dim a,b,c,d,i as integer
for i = 1 to len(text2.text)
if mid(text2.text,i,1)="A" then a=a+1
if mid(text2.text,i,1)="B" then b=b+1
if mid(text2.text,i,1)="C" then c=c+1
if mid(text2.text,i,1)="D" then d=d+1
next i
text1.text="字符A出现次数为" & a & VbLf &"字符B出现次数为" & b & VbLf &"字符C出现次数为" & c & VbLf &"字符D出现次数为" & d
end sub
Private Sub Form_Click()
Dim a,b,c,d,i as integer
for i = 1 to len(text2.text)
if mid(text2.text,i,1)="A" then a=a+1
if mid(text2.text,i,1)="B" then b=b+1
if mid(text2.text,i,1)="C" then c=c+1
if mid(text2.text,i,1)="D" then d=d+1
next i
text1.text="字符A出现次数为" & a & VbLf &"字符B出现次数为" & b & VbLf &"字符C出现次数为" & c & VbLf &"字符D出现次数为" & d
end sub
- 4楼网友:鸽屿
- 2021-03-03 14:44
第一种方法:使用split
temp = txt.text
arrA = split(temp,"A") ’---产生数组
ACount = UBound(arrB)-1 ‘---数组的长度
arrB = split(temp,"B")
BCount = UBound(arrB)-1
。。。。
第二种:使用正则表达式,引用里增加VBScript
ACount = RegExpTest("A",temp)
BCount = RegExpTest("B",temp)
CCount = RegExpTest("C",temp)
Function RegExpTest(patrn, strng)
Set regEx = CreateObject("VBScript.RegExp") '建立正则表达式对象
regEx.Pattern = patrn '设置模式
regEx.IgnoreCase = True '设置是否区分字符大小写
regEx.Global = True '设置全局可用性
Set matches = regEx.Execute(strng) '执行搜索
nCount = matches.count
RegExpTest = nCount ’--返回
End Function
第三种:使用 instr搜索,mid截取,while循环
ACount = GetCount(temp,"A")
BCount = GetCount(temp,"B")
CCount = GetCount(temp,"C")
function GetCount(temp,str1)
temp0 = temp
k = instr(temp0,str1) '--定位字符位置
ncount = 0
while k>0
temp0=mid(temp0,k+1) ’--截取字符串
k = instr(temp0,str1) ‘--重新定位字符位置
ncount = ncount + 1 ’--出现册数
loop
GetCount = nCount ‘--返回出现次数
End Function
temp = txt.text
arrA = split(temp,"A") ’---产生数组
ACount = UBound(arrB)-1 ‘---数组的长度
arrB = split(temp,"B")
BCount = UBound(arrB)-1
。。。。
第二种:使用正则表达式,引用里增加VBScript
ACount = RegExpTest("A",temp)
BCount = RegExpTest("B",temp)
CCount = RegExpTest("C",temp)
Function RegExpTest(patrn, strng)
Set regEx = CreateObject("VBScript.RegExp") '建立正则表达式对象
regEx.Pattern = patrn '设置模式
regEx.IgnoreCase = True '设置是否区分字符大小写
regEx.Global = True '设置全局可用性
Set matches = regEx.Execute(strng) '执行搜索
nCount = matches.count
RegExpTest = nCount ’--返回
End Function
第三种:使用 instr搜索,mid截取,while循环
ACount = GetCount(temp,"A")
BCount = GetCount(temp,"B")
CCount = GetCount(temp,"C")
function GetCount(temp,str1)
temp0 = temp
k = instr(temp0,str1) '--定位字符位置
ncount = 0
while k>0
temp0=mid(temp0,k+1) ’--截取字符串
k = instr(temp0,str1) ‘--重新定位字符位置
ncount = ncount + 1 ’--出现册数
loop
GetCount = nCount ‘--返回出现次数
End Function
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯