473,657 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Contains for string?

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

Nov 21 '05 #1
17 8117
Is String.IndexOfA ny() what you are looking for?

"Tom" <to*@nospam.com > wrote in message
news:Oe******** ******@TK2MSFTN GP11.phx.gbl...
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


Nov 21 '05 #2
Tom,
As Shiva suggests, you can use String.IndexOfA ny, something like:
If strTest.IndexOf Any("ABC".ToCha rArray) <> -1 Then
Debug.WriteLine ("Found characters")
Else
Debug.WriteLine ("Did NOT find characters!")
End If
Or

If strTest.IndexOf Any(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.IndexOf Any(anyOf) <> -1 Then

Hope this helps
Jay

"Tom" <to*@nospam.com > wrote in message
news:Oe******** ******@TK2MSFTN GP11.phx.gbl... 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

Nov 21 '05 #3
Tom
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

"Shiva" <sh******@onlin e.excite.com> wrote in message
news:uj******** ******@TK2MSFTN GP11.phx.gbl...
Is String.IndexOfA ny() what you are looking for?

"Tom" <to*@nospam.com > wrote in message
news:Oe******** ******@TK2MSFTN GP11.phx.gbl...
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

Nov 21 '05 #4
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
Nov 21 '05 #5
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|sl eep"
Static theRegEx As New System.Text.Reg ularExpressions .Regex(pattern,
Text.RegularExp ressions.RegexO ptions.Compiled )
If theRegEx.IsMatc h(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.Reg ularExpressions .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

"Tom" <to*@nospam.com > wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
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

"Shiva" <sh******@onlin e.excite.com> wrote in message
news:uj******** ******@TK2MSFTN GP11.phx.gbl...
Is String.IndexOfA ny() what you are looking for?

"Tom" <to*@nospam.com > wrote in message
news:Oe******** ******@TK2MSFTN GP11.phx.gbl...
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


Nov 21 '05 #6
Doh!,
Dim pattern As String = String.Join("|" , New String() {"cat",
"dog", "sleep"})
If System.Text.Reg ularExpressions .Regex.IsMatch( pattern, strTest)
Reversed the patterns, it should be:

If Regex.IsMatch(s trTest, pattern) Then

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:ux******** ******@tk2msftn gp13.phx.gbl... 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|sl eep"
Static theRegEx As New
System.Text.Reg ularExpressions .Regex(pattern,
Text.RegularExp ressions.RegexO ptions.Compiled )
If theRegEx.IsMatc h(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.Reg ularExpressions .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

"Tom" <to*@nospam.com > wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
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

"Shiva" <sh******@onlin e.excite.com> wrote in message
news:uj******** ******@TK2MSFTN GP11.phx.gbl...
Is String.IndexOfA ny() what you are looking for?

"Tom" <to*@nospam.com > wrote in message
news:Oe******** ******@TK2MSFTN GP11.phx.gbl...
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



Nov 21 '05 #7
"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/>
Nov 21 '05 #8
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******** ******@TK2MSFTN GP12.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/>

Nov 21 '05 #9
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> schrieb:
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 K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
14131
by: Aleksi Kallio | last post by:
I want to say something like this: <xsl:if test="contains($my-string, {'banana', 'apple', 'lemon'})"> Ie. I want to do "contains" against many strings. I don't want to write them all manually to condition clause, because it's cumbersome and it would help a lot if those strings could be passed into template as a parameter. What would be the easiest way to do this?
2
2020
by: Piotr Szukalski | last post by:
Hi! I have trouble with 'Contains' method in ListViewItemCollection class - it seems like it nevers calls 'Equals' method of class inherited from ListViewItem... I've found that ListViewItem doesn't override 'Equals' method, but I _DO_ override it in my class. Have a look at thread:...
2
2651
by: Dot net work | last post by:
Hello, My simple code is here: Public Class MyDictionary Inherits System.Collections.DictionaryBase Private Class MyElement Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean End Function
13
14972
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
14
21760
by: Ralf Rottmann \(www.24100.net\) | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). Assume I want to select rows from a database and check whether a specific column contains keywords from a list of keywords. The following works just fine: List<stringsearchTerms = new List<string>() { "Maria", "Pedro" };
4
10268
by: Jeff | last post by:
Hey ..NET 3.5 I'm trying to search a string to determine if the string contains </table>, but string.Contains don't find it. I've used WebRequest/WebReponse to retrieve the html from a webpage and now I'm searching through this html... this webpage contains 4 html tables. so there are 4 </table>, but my code don't find any...
8
1369
by: Tanzen | last post by:
I'm working in visual studio 2005 trying to learn visual basic. Having come from an VB for Access background, I'm finding it a big learning curve. I have been working through several e-books which have shown how to use custom collections to store lists of data. I've coded the custom collection and before I add members to it, I want to use the contains method to ensure the data does not already exist in the collection. My play code is...
1
2034
by: Jon Skeet [C# MVP] | last post by:
On Apr 30, 3:56 pm, Raja <rajesh.mad...@gmail.comwrote: Well, in fact it's *not* working fine - it's not behaving the same on SQL server as it would be in normal code. String.Contains is case- sensitive. My guess is that your database is set to be case- insensitive, hence why it's behaving how you want it to. To get a case-insensitive Contains equivalent in normal code (which includes LINQ to XML - the delegate is being executed...
8
4654
by: SMJT | last post by:
Does anyone know why the string contains function always returns true if the token is an empty string? I expected it to return false. "AnyOldText".Contains("") or "AnyOldText".Contains(String.Empty)
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.