下面一个利用InStr函数计算字符串出现的次数的函数:Public Function CountSubstrings(target As String, _
template As String, CaseSensitive As Boolean) _
As Integer' Returns the number of times template occurs in target.
' Returns -1 if either string is blank or if templateis longer than target. If CaseSensitive is true, performs a case-sensitive comparison for text. If false, the comparison is case-insensitive.
Dim pos1 As Integer, pos2 As Integer, count As IntegerIf Len(target) = 0 Or Len(template) = 0 Or _
Len(template) > Len(target) Then
CountSubstrings = -1
Exit Function
End If count = 0
pos2 = 1
Do
If CaseSensitive Then
pos1 = InStr(pos2, target, template, vbBinaryCompare)
Else
pos1 = InStr(pos2, target, template, vbTextCompare)
End If
If pos1 > 0 Then
count = count + 1
pos2 = pos1 + 1
End If
Loop Until pos1 = 0CountSubstrings = countEnd Function
我现在有两个文本框 Text1 和 Text2 ,如何调用此函数,使得可以统计出 Text2 中出现了几次 Text1 中的内容,谢谢了
template As String, CaseSensitive As Boolean) _
As Integer' Returns the number of times template occurs in target.
' Returns -1 if either string is blank or if templateis longer than target. If CaseSensitive is true, performs a case-sensitive comparison for text. If false, the comparison is case-insensitive.
Dim pos1 As Integer, pos2 As Integer, count As IntegerIf Len(target) = 0 Or Len(template) = 0 Or _
Len(template) > Len(target) Then
CountSubstrings = -1
Exit Function
End If count = 0
pos2 = 1
Do
If CaseSensitive Then
pos1 = InStr(pos2, target, template, vbBinaryCompare)
Else
pos1 = InStr(pos2, target, template, vbTextCompare)
End If
If pos1 > 0 Then
count = count + 1
pos2 = pos1 + 1
End If
Loop Until pos1 = 0CountSubstrings = countEnd Function
我现在有两个文本框 Text1 和 Text2 ,如何调用此函数,使得可以统计出 Text2 中出现了几次 Text1 中的内容,谢谢了
解决方案 »
- 利用Adodb.Stream验证从网上下载到本地的Web页编码类型是否为UTF-8或GB2312时遇到的问题
- 我调用PrinterProperties后,打印机属性框闪了一下就没了?怎么回事?50分
- 怎樣完美的用ActiveReport2導出到Excel?
- 当窗体最小化时,触发什么事件?(我需要在窗体最小化时做一些事)
- 在VB中想调用DOS来运行pkunzip.exe,解压缩加了密码的zip压缩文件,大虾请给出代码小弟不甚感激.
- 连接数据库问题
- 请问有没有vb控件的详细介绍,谢谢您
- 一个小问题,怎样在VB中生成随机的颜色啊
- 请问怎么在vb中调用纯c接口的api并实现多线程
- 如何修改vertmenu.ocx中显示的文字的大小
- VB6.0恢复界面
- 送分100分:三个表的左连接语句如何写
True还是False自己看着办吧
函数或接口标记为限制的, 或函数使用了 Visual Basic 中不支持的自动类型
这部分我觉得最好还是再优化一下,比如:
pos2 = pos1 + len(template)