Contains for string?

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

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
 
Is String.IndexOfAny() what you are looking for?

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
 
Tom,
As Shiva suggests, you can use String.IndexOfAny, something like:
If strTest.IndexOfAny("ABC".ToCharArray) <> -1 Then
Debug.WriteLine("Found characters")
Else
Debug.WriteLine("Did NOT find characters!")
End If

Or

If strTest.IndexOfAny(New Char() {"A"c, "B"c, "C"c}) <> -1 Then

NOTE: "A"c is a char literal, while "A" is a string literal.

Depending on how often I was using the above, I would consider making the
char array Static to the routine.

Static anyOf() As Char = New Char() {"A"c, "B"c, "C"c}
If strTest.IndexOfAny(anyOf) <> -1 Then

Hope this helps
Jay
 
Shiva: Yes, that works for CHARACTERs, but not for strings. I probably
should have made my example
as such:

strTest = "The cat jumped over the sleepy dog."
If strTest Contains ("cat","dog", "sleep") Then
Debug.WriteLine("Found strings")
Else
Debug.WriteLine("Did NOT find strings!")
End If

I -could- write my own function easily enough for this, but it seems like
the framework should have a similar string function somewhere.

Tom
 
Tom,

You cannot, you should or use a Regex or use a loop with the normal indexof

Where the performance decission when to use the regex is in my opinion about
20 words to search for.

Cor
 
Tom,
I think you are going to have to create your own routine...

I would consider using a RegEx in that routine.

Const pattern As String = "cat|dog|sleep"
Static theRegEx As New System.Text.RegularExpressions.Regex(pattern,
Text.RegularExpressions.RegexOptions.Compiled)
If theRegEx.IsMatch(strTest) Then
Debug.WriteLine("Found strings")
Else
Debug.WriteLine("Did NOT find strings!")
End If

Of course if "cat","dog", "sleep" are dynamic, you could build the pattern
with String.Join:

Dim pattern As String = String.Join("|", New String() {"cat", "dog",
"sleep"})
If System.Text.RegularExpressions.Regex.IsMatch(pattern, strTest)
Then

Which I used would depending on the whether or not the strings I was looking
for were static or dynamic & how often the routine was being called...

Hope this helps
Jay
 
Doh!,
Dim pattern As String = String.Join("|", New String() {"cat",
"dog", "sleep"})
If System.Text.RegularExpressions.Regex.IsMatch(pattern, strTest)

Reversed the patterns, it should be:

If Regex.IsMatch(strTest, pattern) Then

Jay
 
Tom said:
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,
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
 
Jay,

Jay B. Harlow said:
You raised an interesting question. Does Tom want to know
if strTest contains all three or one of?

First I tested if 'String' contains a 'Contains' method, but it doesn't
contain such a method.

BTW: My solution can be simply adapted by replacing 'AndAlso' with 'OrElse'
in order to check for the occurance of one (or more) of the substrings in
the string.
Its harder with RegEx to check to see if it contains all
three...

Especially for longer strings, the way I propagate may be inefficient, but I
don't know how Regex is implemented...

Wondering too...
 
Herfried,
Especially for longer strings, the way I propagate may be inefficient, but
I don't know how Regex is implemented...

Longer input or longer list of words?

RegEx has no syntax that states "this & that" per se (you would need to
create a convoluted pattern), it only has "this or that" (via "this|that")
so checking for one of significant number of words with a regex is
relatively easy (see my other post). Checking for all of a list of words
with RegEx would be significantly hard!

As I've stated before, I would not worry about performance until the routine
was proven to be a performance problem. I would go with one of the two RegEx
I gave earlier based on if the list of words was static or dynamic... If the
RegEx proved to have a performance problem then I would replace the
Algorithm with a more efficient one... (if one could be found that was truly
more efficient in the context of the routine).

Hope this helps
Jay
 
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 said:
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: Yep, I can do it this way, and that is pretty much how I have been
doing it. I just thought there had to be some other intrinsic built-in way
of doing it without having to write the code. That would be a good function
to have in the .NET framework.

I wonder if using a RegEx or using your method would be faster?

Tom
 
Tom,

That is very easy that MVB "Instr" method is the fastest for finding
"strings", it outclasses all other methods.

We have made some testing in past about this in this newsgroup, here is the
thread

http://tinyurl.com/3z2nk

Cor

Tom said:
Herfried: Yep, I can do it this way, and that is pretty much how I have
been
doing it. I just thought there had to be some other intrinsic built-in way
of doing it without having to write the code. That would be a good
function
to have in the .NET framework.

I wonder if using a RegEx or using your method would be faster?

Tom
 
Cor Ligthert said:
That is very easy that MVB "Instr" method is the fastest for finding
"strings", it outclasses all other methods.

We have made some testing in past about this in this newsgroup, here
is the thread

http://tinyurl.com/3z2nk

I think the theoretically fastest implementation would be an adapted version
of the Boyer Moore algorithm or the KMP pattern matching algorithm. By
calling 'InStr' three times (for looking for the occurance of three strings
in the text) there may be comparisons done that don't need to be done with a
specialized implementation.
 
Herfried,

I was thinking about just making a sample method as is the indexofArray and
than with strings, cannot be that hard when you everytime move the index of
a string one position and test it than against the string when the position
is less than the lenght of the search word.

Than would the instr be faster than the indexof, however it would be a
little bit strange routine when I would make this, because I would use the
substring instead of the Mid.

Both should be very easy to be done and probably be the fastest routine, and
when you are sure it are bytes and no unicodes even faster to be done with a
bytearray.

However with methods in previouw message I did mean all methods which are
standard in dotnet as regex, indexof, etc.

Cor
 
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 said:
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
 
Thanks, Jay, this helped greatly.

Tom

Jay B. Harlow said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top