我现在有一个文本文件a.txt里面放了几个string(每行一个),我要在另一个文本文件b.txt中分别判断是否存在a.txt里面的string,若存在,输出b.txt中包含该string所在行的内容,

解决方案 »

  1.   

    没有调试,应该没问题:Function filetxt(txtpath As String) As String
         filetxt = String(FileLen(txtpath), " ")
         Open txtpath For Binary As #1
         Get #1, , filetxt
         Close 1
       End FunctionPrivate 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
      

  2.   

    测试通过aa.txt文本内容如下
    string
    soho
    liuningbb.txt 中文本内容如下
    wangbinghao
    liuhang输出结果:sohohaoSub 查找文本(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 SubPrivate Sub Command1_Click()
        查找文本 "d:\aa.txt", "d:\bb.txt"
    End Sub
      

  3.   

    //以下内容来自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"
      

  4.   

    其实只需要一个循环:
    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 SubFunction 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