急!字符串查找的问题!难题!在线等待

zrsmichael 2003-12-09 11:21:42
我现在有一个文本文件a.txt里面放了几个string(每行一个),我要在另一个文本文件b.txt中分别判断是否存在a.txt里面的string,若存在,输出b.txt中包含该string所在行的内容,
...全文
50 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zrsmichael 2003-12-10
  • 打赏
  • 举报
回复
急呀,有谁知道吗?
aoenzh 2003-12-10
  • 打赏
  • 举报
回复
up
of123 2003-12-10
  • 打赏
  • 举报
回复
其实只需要一个循环:
Private Sub GetIt()
Dim temp as String
Dim a() as String
Dim i As Long
Dim j As Long
tmp = filetxt(App.Path & "\a.txt
a = Split(filetxt(App.Path & "\b.txt"), vbCrLf)
For i = 0 To UBound(b)
If Instr(1, tmp, a(i) & vbcrlf)>0 then
Debug.Print a(i)
Endif
Next
End Sub

Function filetxt(txtpath As String) As String
Dim strTmp as String

On error goto EH
Open txtpath For Input As #1
Get #1, , strTmp
Close 1
filetxt = strTmp
EH:
End Function
踏平扶桑 2003-12-10
  • 打赏
  • 举报
回复
//以下内容来自http://webhost.5ewy.com
Split()以某字串将字串拆成数组
--------------------------------------------------------------------------------
●VB6提供了许多的好用字串处理函数,使得千千VB站的自制函数单元已经如同虚设了。
●用法:传回数组 = Split(原始字串, 要找的字串, 拆成几个数组)

Private Sub Command1_Click()
Dim MyStr As String
MyStr = "1234567123456712345"
MyStrs = Split(MyStr, "67")
For Each Strs In MyStrs
Print Strs
Next
End Sub
●输出结果:"12345"、"12345"、"12345"
 

Private Sub Command1_Click()
Dim MyStr As String
MyStr = "1234567123456712345"
MyStrs = Split(MyStr, "67", 2)
For Each Strs In MyStrs
Print Strs
Next
End Sub
●输出结果:"12345"、"123456712345"
 

Private Sub Command1_Click()
Dim MyStr As String
MyStr = "1234567890123456789012345678901234567890"
Print Replace(MyStr, "90", "九零", 11, 2)
End Sub
●输出结果:"123456789012345678九零12345678九零1234567890"
SoHo_Andy 2003-12-10
  • 打赏
  • 举报
回复
测试通过

aa.txt文本内容如下
string
soho
liuning

bb.txt 中文本内容如下
wangbinghao
liuhang

输出结果:sohohao

Sub 查找文本(strFirstFile As String, strSecondFile As String)
Dim filenum As Integer
Dim fileContents As String
Dim fileInfo1() As String
Dim fileInfo2() As String
Dim i As Integer
Dim j As Integer

filenum = FreeFile
Open strFirstFile For Binary As #filenum
fileContents = Space(LOF(filenum))
Get #filenum, , fileContents
Close filenum
fileInfo1 = Split(fileContents, vbCrLf)
'取出源文件行数,按照回车换行来分隔成数组

fileContents = ""
filenum = FreeFile
Open strSecondFile For Binary As #filenum
fileContents = Space(LOF(filenum))
Get #filenum, , fileContents
Close filenum
fileInfo2 = Split(fileContents, vbCrLf)

Dim mPos As Long
'循环每一行
For i = 0 To UBound(fileInfo1) - 1
For j = 0 To UBound(fileInfo2) - 1
mPos = InStr(1, fileInfo2(j), fileInfo1(i), vbTextCompare)
If mPos <> 0 Then
Debug.Print fileInfo2(j)
End If
Next
Next
MsgBox "完毕"
End Sub

Private Sub Command1_Click()
查找文本 "d:\aa.txt", "d:\bb.txt"
End Sub
northwolves 2003-12-10
  • 打赏
  • 举报
回复
没有调试,应该没问题:

Function filetxt(txtpath As String) As String
filetxt = String(FileLen(txtpath), " ")
Open txtpath For Binary As #1
Get #1, , filetxt
Close 1
End Function

Private Sub Form_Load()
Dim a, b, temp
Dim i As Long
Dim j As Long
a = Split(filetxt(App.Path & "\a.txt"), vbCrLf)
b = Split(filetxt(App.Path & "\b.txt"), vbCrLf)
For i = 0 To UBound(a)
temp = Filter(b, a(i))
For j = 0 To UBound(temp)
Debug.Print temp(j)
Next
Next
End Sub
zrsmichael 2003-12-09
  • 打赏
  • 举报
回复
我想要现成代码,很急
rainstormmaster 2003-12-09
  • 打赏
  • 举报
回复
提示:
用instr函数

7,762

社区成员

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

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