Link to home
Start Free TrialLog in
Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America

asked on

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
SOLUTION
Avatar of Chirag1211
Chirag1211
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mr_Fulano

ASKER

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
I don't see any other method.
Avatar of Sancler
Sancler

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
>> 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
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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