经典ASP - 尝试在另一个字符串变量中搜索字符串。 (替代InStr功能)

时间:2017-07-15 16:58:41

标签: string vbscript asp-classic

在以下代码currentregion = 'BC'Regions = 'ABC'

我想在currentregion中找到Regions,通常它应该点击else,因为if条件是false

但它返回true,因为InStr搜索字符串的一部分而不是字符串vs字符串。因此BC成为ABC的一部分,它进入了if循环。

在Classic ASP中是否有一个函数我可以将字符串与字符串进行比较,但不像InStr函数那样将字符串的一部分进行比较。

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

3 个答案:

答案 0 :(得分:2)

尝试使用

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

而不是

If InStr(Regions,currentregion) > 0 Then

答案 1 :(得分:1)

Split首先将字符串放入数组中,然后循环并比较每个元素

答案 2 :(得分:0)

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