Solved

STRING CONTAINS

Posted on 2013-01-03
13
207 Views
Last Modified: 2013-01-11
Hi All,

I have a string and I want to compare if in ("A", "B", "C").
I try VariableA.Contains("A", "B", "C"), it does not work.

How could I do it using string.contains ?  


Or there are other ways to do it ?

Thank you.
0
Comment
Question by:emi_sastra
13 Comments
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 38742984
you can only compare it one at a time. Sample:

If variableA.Contains("A") then
      'Do something here
End if

If variableA.Contains("B") then
      'Do something here
End if

If variableA.Contains("C") then
      'Do something here
End if
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 38742992
VariableA.Contains(strToCompareVariable)

The strToCompareVariable is already "A", "B", "C", and I get it from a function.

Is there any simple ways to do it ?

Thank you.
0
 
LVL 8

Accepted Solution

by:
PagodNaUtak earned 250 total points
ID: 38742995
Or you can do something like this:

Dim VairableA As String = "JosephOcena"
        Dim Search() As Char = "A,B,C"

        Dim result = (From letter In VairableA.ToUpper _
                     Where Search.Contains(letter)).Count


        MsgBox(result)

Open in new window

0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 38742997
Can you give me the value of VariableA and strToCompareVariable?
0
 
LVL 16

Assisted Solution

by:Peter Kwan
Peter Kwan earned 250 total points
ID: 38743001
A more cleaner way:

If variableA.Contains("A") or variableA.contains("B") or variableA.contains("C") then 
       ' do something
End if

Open in new window


Another method you may consider:
Dim str As String = "A,B,C"
Dim anyOf As Char() = str.ToCharArray()
If str.IndexOfAny(anyOf, 1) > -1 Then
    ' do something
End If

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 38743006
I've got error :

Error      1      Expression of type 'String' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider.       

Thank you.
0
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 38743049
Can you post the code?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 38743071
This is the code that throw compile error.

Dim VairableA As String = "JosephOcena"
        Dim Search() As Char = "A,B,C"

        Dim result = (From letter In VairableA.ToUpper _
                     Where Search.Contains(letter)).Count

Thank you.
0
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 38743132
Add an Import statement for the System.Linq, System.Data.Linq, or System.Xml.Linq namespace to your code file. You can also import namespaces for your project by using the References page of the Project Designer (My Project).
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 38743180
-    Dim result = (From letter In VairableA.ToUpper _
                     Where Search.Contains(letter)).Count

What is letter here ?

Thank you.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38744880
Do you mean

"A,B,C".Contains(VariableA)

?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 38745928
I mean VariableA,contains(strToCompareVariable)

VariableA could be any string.
strToCompareVariable="A, B, C", yet strToCompareVariable could any string too.

Thank you.
0
 
LVL 1

Author Closing Comment

by:emi_sastra
ID: 38769117
Hi All,

Thank you very much for your help.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I need help assigning an image to an image control in my VB.Net WPF project 3 52
Variable Event ? 3 27
VB.NET - Error - Object not set in Module 4 33
VB.NET 1 22
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question