Solved

STRING CONTAINS

Posted on 2013-01-03
13
205 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Can you give me the value of VariableA and strToCompareVariable?
0
 
LVL 16

Assisted Solution

by:Peter Kwan
Peter Kwan earned 250 total points
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 8

Expert Comment

by:PagodNaUtak
Comment Utility
Can you post the code?
0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
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
Comment Utility
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
Comment Utility
-    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
Comment Utility
Do you mean

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

?
0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
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
Comment Utility
Hi All,

Thank you very much for your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

11 Experts available now in Live!

Get 1:1 Help Now