?
Solved

STRING CONTAINS

Posted on 2013-01-03
13
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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