字符串的计数

Murray88 2003-11-21 10:26:39
请教各位大侠:一个长字符串中的相同字符个数怎么计算?
...全文
124 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mi_Bo 2003-11-22
  • 打赏
  • 举报
回复
up
bbe 2003-11-22
  • 打赏
  • 举报
回复
如果s = "12345543211235555678234aaawwwqqwwww1222222333324wsewwerewrew"
s1 = "ww"
你认为个数不是4而是6的话,建议直接使用InStr

Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Sub Command1_Click()
Dim s As String
Dim s1 As String
s = "12345543211235555678234aaawwwqqwwww1222222333324wsewwerewrew"
s1 = "w"
'求s中s1的个数
Dim i As Long, temps As String, j As Long
Dim t As Long
Dim a As Long, b As Long
Dim sum As Long

'(1)
t = GetTickCount
For a = 0 To 10000
temps = Replace(s, s1, "")
i = Len(s) - Len(temps)
i = i / Len(s1)
Next
MsgBox GetTickCount - t, , i

'(2)
t = GetTickCount
For a = 0 To 10000
j = UBound(Split(s, s1))
Next
MsgBox GetTickCount - t, , j

'(3)
t = GetTickCount
For a = 0 To 10000
b = 1
j = 0
Do
i = InStr(b, s, s1)
If i = 0 Then Exit Do
j = j + 1
b = i + 1
Loop
Next
MsgBox GetTickCount - t, , j

End Sub
yaowei2008 2003-11-22
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim s, tempS As String
Dim s1 As String
Dim i, sum As Integer
s = "12345543211235555678234aaawwwqqwwww1222222333324wsewwerewrew"
s1 = "w"
sum = 0

'求s中s1的个数
For i = 1 To Len(s)
tempS = Mid(s, i, 1)
If tempS = s1 Then
sum = sum + 1
End If
Next
'SUM为W的个数

End Sub
captainivy 2003-11-22
  • 打赏
  • 举报
回复
非常同意 野性的呼唤

kimurakenshin 2003-11-22
  • 打赏
  • 举报
回复
UP
northwolves 2003-11-22
  • 打赏
  • 举报
回复
rainstormmaster(rainstormmaster) 兄弟复杂化了:
ubound(split(s,s1)) 就是所求
rainstormmaster 2003-11-21
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim s As String
Dim s1 As String
s = "12345543211235555678234aaawwwqqwwww1222222333324wsewwerewrew"
s1 = "w"
'求s中s1的个数
Dim i As Long, temps As String
temps = Replace(s, s1, "")
i = Len(s) - Len(temps)
i = i / Len(s1)
'i即为所求
End Sub

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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