Classic ASP - Trying to search a string in another string variable. (Alternative to InStr function)

喜你入骨 提交于 2019-12-12 04:21:29

问题


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


来源:https://stackoverflow.com/questions/45120678/classic-asp-trying-to-search-a-string-in-another-string-variable-alternative

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!