Classic ASP - Trying to search a string in another

2019-08-24 13:59发布

问题:

In the following code currentregion = 'BC' and Regions = 'ABC'.

I am trying to find the currentregion in the Regions and normally it should hit else as the if condition is false.

But it returns true as InStr searches part of the string not string vs string. So BC being part of ABC it enters the if loop.

Is there a function in Classic ASP that I can compare a string to string but not part of the string like InStr function.

RegionSQL = "SELECT * FROM Regions Where Auth <= " & Session("U_Auth") & ";"                
Set rsRegion=Server.CreateObject("recordset")
rsRegion.Open RegionSQL,TheDB

If NOT rsRegion.EOF And NOT rsRegion.BOF Then
    rsRegion.MoveFirst
    While Not rsRegion.Eof 
        'Grab Current Region
        currentregion = rsRegion("RegionCodeShort")   

        If InStr(Regions,currentregion) > 0 Then
            checked = "checked"
        Else
            checked = ""
        End If

回答1:

Split the string into an array first, then loop through and compare each element



回答2:

Try using

If strcomp(Regions,currentregion,vbTextCompare) = 0 Then

instead of

If InStr(Regions,currentregion) > 0 Then


回答3:

Classic task for Regular expressions:

Private Sub CommandButton1_Click()
    MsgBox (find("ABC", "(ABC)")) ' exist
    MsgBox (find("BC", "(ABC)")) ' not exist
End Sub

Function find(aString As String, ByVal pattern As String) As Boolean
    Dim regEx As Object
    Set regEx = CreateObject("vbscript.regexp")

    Dim newArray() As String
    Dim cnt As Integer
    regEx.pattern = pattern
    regEx.IgnoreCase = True
    regEx.Global = True

    Set matches = regEx.Execute(aString)
    Dim x As Integer
    x = matches.Count

    If x = 0 Then
        find = False
    Else
        find = True
    End If

End Function