File "Contains" method

Hi,

I'm searching a text file for any instance of a string using the myfile.Contains("mystring") method. The file is a simple text file, containing one word per line and then the next word follows on the next line after that, and so on to the EOF. However, I'm running into the situation where if I'm searching for something like the string "write", I'm also finding it in the word "rewrite" as well. Clearly, "Contains" simply searches the file and determines if the string is contained therein.

1). How can I force the Contain method to begin at the start of each line.
and
2). How can I tell it that I do not want sub-strings contained in larger stings.  

-- Both solutions would work and for my edification, if you can explain both I'd appreciate it.  

Since this is a two part question, I'm adding the Max points possible for your efforts.

Thanks,
Fulano
Mr_FulanoAsked:
Who is Participating?
 
SanclerCommented:
This would be an alternative, using [String].Contains within a file-reading wrapper.

Imports System.IO

    Private Function filecontains(ByVal filetosearch As String, ByVal tofind As String) As Boolean
        Dim sr As New StreamReader(filetosearch)
        Dim s As String = vbCrLf & sr.ReadToEnd & vbCrLf
        sr.Close()
        Return s.Contains(vbCrLf & tofind & vbCrLf)
    End Function

As it stands it assumes that the search is to be case and culture sensitive, that the file contains no extraneous spaces and that the linebreaks are vbCrLf.  If any of those assumptions is false it would either need amending or it might not be viable at all.

Roger
0
 
Chirag1211Commented:
0
 
DhaestCommented:
The easiest way is to loop your filelines and then check the first word ...

       Dim oRead As System.IO.StreamReader
        Dim LineIn As String
' Open your file
        oRead = IO.File.OpenText("C:\temp\subparts.txt")
' Read it line by line
        While oRead.Peek <> -1
' Read a line
            LineIn = oRead.ReadLine()
            Dim i As Integer
            i = 0
' Does the line start with "write"
        Dim bValid As Boolean = False
           if LineIn.startswith("write") then
' check if it's one word and not that it fe writer
            If anotherString.Length > 5 Then
                If anotherString.Substring(6) = "write " Then
                    bValid = True
                End If
            Else
                bValid = True
            End If
        End If

        End While

        oRead.Close()
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Mr_FulanoAuthor Commented:
Hi Guys,

Chirag, RE seem to be very interesting, but they may be a little bit outside my league right now. I don't think I know how to use them effectively and they are not really simple to digest. The links you provided are also links to #C coding, which I do not understand very well. Do you have a link to VB coding, which may help me understand them?

Dhaest, your method is good, if I wanted to look through the list line by line. However, the list is quite long (over 84K line), so I didn't want to loop for 84K iterations. I though that Contains would be more efficient if I could force it to behave in a way I could control what it looks for. -- Maybe I'm wrong in thinking that way...and looping for 84K iterations just isn't that big of a deal.

Thanks,
Fulano
0
 
DhaestCommented:
I don't see any other method.
0
 
SanclerCommented:
Can you please give a bit more detail about the myfile.Contains("mystring") method?  I don't recognise it as a standard VB.NET method.  And without knowing what exactly it is, it is difficult to suggest how it might be used in the way you want.

Roger
0
 
DhaestCommented:
>> I don't recognise it as a standard VB.NET method

It is a standard .NET-method of a string:
see http://msdn2.microsoft.com/en-us/library/system.string.contains.aspx
0
 
SanclerCommented:
Yes, but myfile does not appear, on its face, to be a string (and there are scores of other .Contains methods for other objects - although I agree that [String].Contains is far the most likely).  But it means that, if a native VB.NET method is being used it must be wrapped in other code - e.g. to open the file and read it into some object, or even series of objects, on which the method can be applied.  And if we are to answer the questions

>>
1). How can I force the Contain method to begin at the start of each line.
and
2). How can I tell it that I do not want sub-strings contained in larger stings.  
<<

I think we need to know what the "method", as a whole, is.

This does not mean that I disagree with your suggestions about what may be the best approach for achieving the end objective.   But that, in effect, says "replace your current .Contains method with this".  Before actually saying that, I'd like to know what the current .Contains method (as a whole) actually is.

Roger
0
 
wsh2Commented:
Using a Regular Expression, count the number of times a whole word appears, regardless of where it appears in a line:
-----------------------------------------------------------------------------------------------
   Private Function FileCountOfWholeWords( _
   ByVal v_strFilePath As String, _
   ByVal v_strToFind As String _
   ) As Integer
   '
      Dim strBuffer As String = ""
      Using srGet As New System.IO.StreamReader(v_strFilePath)
         strBuffer = srGet.ReadToEnd
      End Using
   '
      Dim objRegex As New System.Text.RegularExpressions.Regex( _
         "\b" & Trim(v_strToFind) & "\b", _
         System.Text.RegularExpressions.RegexOptions.IgnoreCase Or _
         System.Text.RegularExpressions.RegexOptions.Multiline)
      Return objRegex.Matches(strBuffer).Count
   '
   End Function
-----------------------------------------------------------------------------------------------
If you only want a count of lines that begin with the whole word:

   "^" & Trim(v_strToFind) & "\b"

If you only want a count of lines that contain only the whole word (and nothing else):

   "^" & Trim(v_strToFind) & "$"
0
 
Mr_FulanoAuthor Commented:
Gentlemen, first and foremost, thank you for your help!

I reviewed each of your suggestions and I came to a decision as to which would best fit my immediate needs.

1). Chirag1211's and WSH2's suggestions of using "Regular Expressions" were nice ideas and a good learning experience, but not what I really wanted, because they didn't work exactly like I expected - I may have messed up, but I'm not experienced enough to trouble shoot this code just yet!

2). Dheast's loop suggestions was again a good suggestions, but I specifically didn't want to loop through 84K lines over and over again.

3). Sancler (Roger)'s suggestions was mostly what I was looking for, but I could not get his code to work in my scenario. However, Roger was correct in pointing out that a lot of things had to be precise for the code to work and an unexpected space in the wrong place, may be problematic. So, I adapted his idea to one that would work for me.

I added an underscores ( _ ) on either side of each of my strings in my text file. That way I was sure of what my delimiters were and I could use them in my code to pin-point where I wanted the search to begin and end. This (although its not the most elegant method) worked for me and gives me a little peace-of-mind. My line of code became:

If strMyTextString.Contains("_" + strSubjectString.ToLower + "_") = False Then
(do something...)
End If

For these reasons I split the points up in the following manner:

Chirag1211 = 75
Dhaest = 50
Sancler = 300  < I used more of Roger's suggestions than the others...
WSH2 = 75

Thanks to all for the help,
Fulano
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.