Solved

STRING CONTAINS

Posted on 2013-01-03
13
208 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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