问vb简单文本查找

Kevin.Y.K 2008-11-10 09:19:30
本人是初学者,想实现一个简单文本查找,遇到一些问题:
在text1中为原始文本字符串,text3中为要查找的字符串,需要在text4中显示查找的字符串出现的次数,另有一个查找按钮Command1
这是我写的程序:
Private Sub Command1_Click()
Dim pos1 As Single, pos2 As Single, count As Single, Str As String, selStr As String
Str = Text1.Text
selStr = Text3.Text
pos1 = 1
count = 0
Do While pos1 > 0
pos2 = InStr(pos1, Str, selStr)
If pos2 = 0 Then
ccount = count
Else
count = count + 1
End If
pos1 = pos2
Loop
Text4.Text = count
End Sub

好像进入了死循环,各位高手帮我看下,谢谢拉!!!
...全文
123 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
东方之珠 2008-11-10
  • 打赏
  • 举报
回复
楼上的是VB.net代码.用split函数是很简单的。翻译成VB6:

Option Explicit

Private Sub Command1_Click()
'计算selStr在Str中出现的次数
Dim Str As String, selStr As String
Dim Arr() As String
Str = Text1.Text
selStr = Text3.Text
Arr = Split(Str, selStr)
Text4.Text = UBound(Arr)
End Sub


Private Sub Form_Load()
Text1.Text = "exTextTextTextTextTextTextTextTextTextTextex"
Text3.Text = "ex"
Text4.Text = ""
End Sub
Forrest23 2008-11-10
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
    Dim str As String
Dim col As String
Dim pos1, pos2, count As Single
str = Me.Text1.Text
col = Me.Text2.Text
count = 0
pos1 = 1
Do While pos1 > 0
pos2 = InStr(pos1, str, col)
If pos2 = 0 Then
Exit Do
Else
count = count + 1
End If
pos1 = pos2 + Len(col)
Loop
Me.Text3.Text = count
东方之珠 2008-11-10
  • 打赏
  • 举报
回复
死循环的原因是: ccount = count 换成:exit do
另外:下一次寻找位置换成:Pos1 = Pos2 + Len(selStr)
Forrest23 2008-11-10
  • 打赏
  • 举报
回复
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str, col As String
Dim st() As String
col = Me.TextBox2.Text.ToString.Trim '要查找的字符串
str = Me.TextBox1.Text.ToString.Trim '原始文本字符串
st = str.Split(col)
Me.TextBox3.Text = st.Length - 1 '字符串出现的次数
End Sub
东方之珠 2008-11-10
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Command1_Click()
Dim Pos1 As Single, Pos2 As Single, Count As Single, Str As String, selStr As String
Str = Text1.Text
selStr = Text3.Text
Pos1 = 1
Count = 0
Do While True
Pos2 = InStr(Pos1, Str, selStr)
If Pos2 = 0 Then
Exit Do Else
Count = Count + 1
End If
Pos1 = Pos2 + Len(selStr)
Loop
Text4.Text = Count
End Sub
ZOU_SEAFARER 2008-11-10
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim pos1 As Single, pos2 As Single, count As Single, Str As String, selStr As String
Str = Text1.Text
selStr = Text3.Text
pos1 = 1
count = 0
Do While pos1 > 0

pos2 = InStr(pos1, Str, selStr)
If pos2 = 0 Then
ccount = count
Else
count = count + 1
End If
pos1 = pos2+ len(selstr) //设定下一次查询的起始位置
或者截断结果以前的字符串,
Loop
Text4.Text = count
End Sub
zuoxingyu 2008-11-10
  • 打赏
  • 举报
回复
这个问题问过好多次了

自己搜搜吧
Kevin.Y.K 2008-11-10
  • 打赏
  • 举报
回复
非常感谢各位,因为刚学,所以有很多还不会用!
在此特别感谢chenjl1031,虽然帖子没分,还是那么尽力帮助,谢谢!!!
以后也像你学习,顶你O(∩_∩)O哈哈~

7,785

社区成员

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

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