Re: Contains for string?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Tom (tom_at_nospam.com)
Date: 10/15/04


Date: Fri, 15 Oct 2004 15:11:12 -0400

Thanks, Jay, this helped greatly.

Tom

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
news:u6U41wssEHA.2768@TK2MSFTNGP14.phx.gbl...
> Tom,
> As we all suggested there is no builtin "In" function per se.
>
> I would use the RegEx as its IMHO the "simplest" & "cleanest"
> implementation! However! you need to understand RegEx to be comfortable
> using it, luckily this regex is easy. Also you may need to get over the
> perception that RegEx is slow. Yes it has some inherit overhead the other
> methods do not, however that overhead may be warranted for the simplicity
of
> the routine. Also based on the context the overhead of the RegEx may be
> lower, significantly lower, then other routines.
>
> Note: I would only use the RegEx method for checking Strings, if I was
> creating an "In" for other types I would pick an algorithm that was more
> friendly for that type.
>
> Here's one possibility for an "In" function based on RegEx.
>
> Public Shared Function [In](ByVal input As String, ByVal ParamArray
> words() As String) As Boolean
> Dim pattern As String = String.Join("|", words)
> Return System.Text.RegularExpressions.Regex.IsMatch(input,
pattern)
> End Function
>
> Then to use it you simple need to:
>
> strTest = "The cat jumped over the sleepy dog."
> If [In](strTest, "cat","dog", "sleep") Then
> Debug.WriteLine("Found strings")
> Else
> Debug.WriteLine("Did NOT find strings!")
> End If
>
> If the above RegEx version proved to be a performance problem I would then
> consider using a For Each loop.
>
> Public Shared Function [In](ByVal input As String, ByVal ParamArray
> words() As String) As Boolean
> For Each word As String In words
> If input.IndexOf(word) <> -1 Then
> Return True
> End If
> Next
> Return False
> End Function
>
> I would consider creating an object that contained a list of words, which
> had a method the checked to see if a string had one of those words.
>
> Public Class ValidValues
>
> Private ReadOnly m_regex As System.Text.RegularExpressions.Regex
>
> Public Sub New(ByVal ParamArray words() As String)
> Dim pattern As String = String.Join("|", words)
> m_regex = New System.Text.RegularExpressions.Regex(pattern,
> System.Text.RegularExpressions.RegexOptions.Compiled)
> End Sub
>
> Public Function IsMatch(ByVal value As String) As Boolean
> Return m_regex.IsMatch(value)
> End Function
>
> End Class
>
>
> Dim values As New ValidValues("cat", "dog", "sleep")
> If values.IsMatch(strTest) Then
> Debug.WriteLine("Found strings")
> Else
> Debug.WriteLine("Did NOT find strings!")
> End If
> Return
>
> Dim statusValues As New ValidValues("ACTIVE","OTHER")
> If statusValues.IsMatch(Status) Then
> ...
>
> Notice in both cases that the actual method (algorithm) of matching is
> hidden (encapsulated) within either the object or the function itself.
Which
> allows you to replace the Algorithm with a more efficient one if needed...
>
> Whether I used the Function or Class would depend on how the function or
> class was being within my program... Using Refactoring
> http://www.refactoring.com I can change between the two...
>
> As the others pointed out there are other equally valid ways to implement
> the above. There are also methods that have yet to be mentioned, such as
> using a HashTable or using a DataSet, plus still others. Which one you
> should use REALLY depends on the context of what you are doing!
>
> For example if your status value is in a DataTable, you can use a filter
> statement that include an "In" statement, on either DataTable.Select or
> DataView.RowFilter. Like wise of the list of valid statues are in a
> DataTable, you could use DataTable.Rows.Find assuming that the status was
> the primary key to that table, or use filter & either DataTable.Select or
> DataView.RowFilter...
>
> Hope this helps
> Jay
>
>
>
>
> "Tom" <tom@nospam.com> wrote in message
> news:%23bJRd0qsEHA.3572@tk2msftngp13.phx.gbl...
> > No, actually all I would want to know is if any of the strings exists in
> > the
> > line. I want it to work pretty much the way the SQL IN verb works. i.e.
> > where Status in ('ACTIVE','OTHER')
> >
> > Which brings up an interesting question: Is there any kind of IN verb
> > anywhere in VB.NET or the .NET framework?
> >
> > But thanks for all the suggestions; I think I will try to make up a
> > function
> > or something that I can call to do this.
> >
> > Tom
> >
> > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in
message
> > news:uQjSShisEHA.904@TK2MSFTNGP11.phx.gbl...
> >> Herfried,
> >> You raised an interesting question. Does Tom want to know if strTest
> >> contains all three or one of?
> >>
> >> Its harder with RegEx to check to see if it contains all three...
> >>
> >> Wondering
> >> Jay
> >>
> >> "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
> >> news:OD2TCfisEHA.2320@TK2MSFTNGP12.phx.gbl...
> >> > "Tom" <tom@nospam.com> schrieb:
> >> >> Is there such a thing as a CONTAINS for a string variable in VB.NET?
> > For
> >> >> instance, I want to do something like the following:
> >> >>
> >> >> If strTest Contains ("A","B", "C") Then
> >> >> Debug.WriteLine("Found characters")
> >> >> Else
> >> >> Debug.WriteLine("Did NOT find characters!")
> >> >> End If
> >> >>
> >> >
> >> > For reasons of readability, I prefer this solution:
> >> >
> >> > \\\
> >> > Dim s As String = _
> >> > "Quidquid id est timeo Danaos et dona ferentes."
> >> > If _
> >> > InStr(s, "Quidquid") AndAlso _
> >> > InStr(s, "timeo") AndAlso _
> >> > InStr(s, "dona") _
> >> > Then
> >> > MsgBox("True")
> >> > Else
> >> > MsgBox("False")
> >> > End If
> >> > ///
> >> >
> >> > --
> >> > Herfried K. Wagner [MVP]
> >> > <URL:http://dotnet.mvps.org/>
> >>
> >>
> >
> >
>
>



Relevant Pages

  • Re: Fastest way to search a string for the occurance of a word??
    ... but the OP's question was what's the "Fastest way to search a string ... in all the tests I did here, the Regex was by far superior. ... However, of course, if you've got new regular expressions all ... Sure - but just that extra Match object could be relevant if the search ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regular expression help
    ... Basically because if you remove everything that is optional in the regex below you end up with an empty regex: ... So the regex engine will try to match on every character in the string: ... , comma doesn't match, but the nothingness in front of it does. ... A quote followed by any sequence of characters that is not a quote, ...
    (microsoft.public.dotnet.framework)
  • Re: Regex optimization
    ... I was hoping that someone with knowledge of the Regex engine could ... match per string for either Regex. ... reluctant modifier, may be slower .*?, +? ... Variable parts will try to capture as much as possible. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex Capture problem
    ... "learned" my regex using a freeware utility that had slightly different ... was trying to capture instead of. ... I have used Regex utilities before, so I understand the concepts of text ... Function RESub(str As String, SrchFor As String, ReplWith As String) As String ...
    (microsoft.public.excel.programming)
  • Re: Trim a multiple line message to a single line
    ... You can do this quite easily with either a regex or a simple function I'll try to demonstrate both: ... private string LayoutInput ... Could you send a sample file with two of these data blocks f what ...
    (microsoft.public.dotnet.languages.csharp)