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.Compil ed)
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" <to*@nospam.com> wrote in message
news:%2****************@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]" <Ja************@msn.com> wrote in message
news:uQ*************@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]" <hi***************@gmx.at> wrote in message
news:OD**************@TK2MSFTNGP12.phx.gbl... > "Tom" <to*@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/>