Link to home
Start Free TrialLog in
Avatar of SETP
SETP

asked on

Checking if a variable is in a list of values

Is there a quicker way to check if a String variable in VB 2005 is in a list of values? For example, instead of the following:

Dim x As String
If x = "Mr" Or x = "Mrs" Or x = "Miss" Or x = "Ms" Or x = "Prof" Or x = "Doc" Then
    'Do something
End If

Isn't there something like this?:

Dim x As String
Dim values() As String = {"Mr", "Mrs", "Miss", "Ms", "Prof", "Doc"}
If x IsIn values Then
    'Do something
End If

Something along those lines?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also play around with regexpressions....
If you break on fieldValues  you can see the filed break down of this item.
from there you can look for different string item that you want.

Sample:
       Dim strTest As String = "Dr. Jane O Smith M.D."
        Dim fieldValues As String() = ParseLine(strTest)

    End Sub
    Private Shared Function ParseLine(ByVal oneLine As String) As String()
        Dim pattern As String = "[ ,]+(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"
        Dim r As System.Text.RegularExpressions.Regex = _
                New System.Text.RegularExpressions.Regex(pattern)
        Return r.Split(oneLine)
    End Function