Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

File "Contains" method

Posted on 2007-08-09
10
Medium Priority
?
331 Views
Last Modified: 2013-11-05
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
0
Comment
Question by:Mr_Fulano
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 9

Assisted Solution

by:Chirag1211
Chirag1211 earned 300 total points
ID: 19667864
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 200 total points
ID: 19667872
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
 

Author Comment

by:Mr_Fulano
ID: 19667932
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 53

Expert Comment

by:Dhaest
ID: 19668276
I don't see any other method.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 19668284
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
 
LVL 53

Expert Comment

by:Dhaest
ID: 19668303
>> 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
 
LVL 34

Expert Comment

by:Sancler
ID: 19668346
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
 
LVL 34

Accepted Solution

by:
Sancler earned 1200 total points
ID: 19668501
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
 
LVL 14

Assisted Solution

by:wsh2
wsh2 earned 300 total points
ID: 19668656
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
 

Author Comment

by:Mr_Fulano
ID: 19675156
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question