Solved

STRING CONTAINS

Posted on 2013-01-03
13
206 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now