Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Last instance in text file and display row 4

Status
Not open for further replies.

chrsab

Programmer
May 24, 2002
44
GB
Hello All

I am currently working on a program which displays text files in a text box.

What i need is to search the text box for the last instance of a word and then display that line.

Is this possible? or do i have to reverse the textbox to show the bottom text at the top and then search for the first instance.

Any help will be much appreciated.

Thank You
 
Take a look at the instr function it returns the location where an occurance of a string occurs within another, add 1 to that and loop until nothing else found. Then take a look at the SelStart and SelLength properties of the Textbox. Let us know if you need more help.


Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Could you simply use a listbox? This way, every file becomes it's own line and you have a quick access to every item. To get the last one, you'd simply use lstFiles.List(lstFiles.ListCount-1)
-Max
 
Dr, Would like to extend what you had posted in another thread.. try this chrsab..

txtFileContents.SelStart = Len(txtFileContents.Text) - Len(Split(txtFileContents.Text, strWordToSearchFor)(UBound(Split(txtFileContents.Text, strWordToSearchFor)))) - Len(strWordToSearchFor)

You will have to add space to the beginning of strWordToSearchFor, if you are searching for a word.

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
>To get the last one, you'd simply use lstFiles.List(lstFiles.ListCount-1)

Yep, that'd get the last line alright - but we're looking for the last line that contains a specific word ...
 
Then simply loop through the list items looking for the word. I'm not saying that this way is better, I'm just saying that it might be easier depending on his setup.
-Max
 
Meanwhile, the one line code posted above is case sensitive. You can make the complete text and the search string ucase or lcase to avoid that.

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
How abut this:

txtFileContents.SelStart = InStrRev(txtFileContents.text, strWordToSearchFor)


Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Much much better..

May be

txtFileContents.SelStart = InStrRev(txtFileContents.Text, strWordToSearchFor) - 1

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Hello

Yes this will select the row which i am looking for. But how would i display say that row in another text box.

Thanx for all your help.
 
Following function may help.
___
[tt]
Function FindLastLine(Text As String, Word As String) As String
Dim N As Long, lStart As Long, lStop As Long
N = InStrRev(Text, Word, , vbTextCompare)
If N = 0 Then Exit Function 'word not found
lStart = InStrRev(Text, vbCrLf, N)
If lStart = 0 Then lStart = 1 Else lStart = lStart + 2
lStop = InStr(N, Text, vbCrLf)
If lStop = 0 Then lStop = Len(Text) + 1
FindLastLine = Mid$(Text, lStart, lStop - lStart)
End Function[/tt]
___

You may use it like this.

[tt]Text2.Text = FindLastLine(Text1.Text, strWordToSearchFor)[/tt]
 
... And here is the RegExp version of the same function. Appears to be working fine. Perhaps strongm or DrJavaJoe can check if it is correct. [smile]
___
[tt]
Function FindLastLine(Text As String, Word As String) As String
Dim re As RegExp, Obj
With CreateObject("VBScript.RegExp")
.Global = True
.IgnoreCase = True
.MultiLine = True
.Pattern = "^.*" & Word & ".*$"
With .Execute(Text)
If .Count Then FindLastLine = .Item(.Count - 1).Value
End With
End With
End Function[/tt]
 
Looks quite close to the one I wrote on Friday (but not posted here because if, as originally posted, we'are just looking for the word, then the InStrRev solution is generally faster; of course, now the OP has revealed he wants the whole line, which makes the RegExp solution much more suitable)
 
In fact, I'll show you how close. The differences are simply that mine allows you to select whether to be case sensitive or not, and also returns the character position where the line starts in case that is useful:
Code:
[blue]Private Function GetLastLineWithWord(strText As String, strWord As String, lCharPos As Long, Optional bMatchWholeWord As Boolean = True, Optional IgnoreCase As Boolean = True) As String
    With CreateObject("vbscript.regexp")
        .Global = True
        .MultiLine = True
        .IgnoreCase = IgnoreCase
        If bMatchWholeWord Then
            .Pattern = "(^.*\b)" & strWord & "(\b.*$)"
        Else
            .Pattern = "(^.*)" & strWord & "(.*$)"
        End If
        With .Execute(strText)
            If .Count Then
                lCharPos = .Item(.Count - 1).FirstIndex
                GetLastLineWithWord = .Item(.Count - 1).Value
            End If
        End With
    End With
End Function[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top