对不起这个问题,我对我的经典 ASP 很生疏,无法理解它。
我有一个包含一系列邮政编码开头的变量:
strPostCode = "HS1,HS2,HS3,HS4,HS5,HS6,HS7,HS8,HS9,IV41,IV42" etc etc
现在我需要查看用户输入的邮政编码是否存在于该字符串中。
所以,“HS2 4AB”、“HS2”、“HS24AB”都需要返回一个匹配项。
有任何想法吗?
谢谢
对不起这个问题,我对我的经典 ASP 很生疏,无法理解它。
我有一个包含一系列邮政编码开头的变量:
strPostCode = "HS1,HS2,HS3,HS4,HS5,HS6,HS7,HS8,HS9,IV41,IV42" etc etc
现在我需要查看用户输入的邮政编码是否存在于该字符串中。
所以,“HS2 4AB”、“HS2”、“HS24AB”都需要返回一个匹配项。
有任何想法吗?
谢谢
您需要用逗号分隔邮政编码,然后逐个查找匹配项。
代码如下所示:
Dim strPostCode, strInput, x
Dim arrPostCodes, curPostCode, blnFoundMatch
strPostCode = "HS1,HS2,HS3,HS4,HS5,HS6,HS7,HS8,HS9,IV41,IV42"
strInput = "HS24AB"
arrPostCodes = Split(strPostCode, ",")
blnFoundMatch = False
For x=0 To UBound(arrPostCodes)
curPostCode = arrPostCodes(x)
'If Left(strInput, Len(curPostCode))=curPostCode Then
If InStr(strInput, curPostCode)>0 Then
blnFoundMatch = True
Exit For
End If
Next
Erase arrPostCodes
If blnFoundMatch Then
'match found, do something...
End If
以上将在用户输入的任何位置查找每个邮政编码,例如“4AB HS2”也将返回匹配项;如果您希望邮政编码仅出现在输入的开头,请使用If
上述代码中标注的替代行。
您可以将搜索字符串拆分为字符,然后在循环中检查该字符是否存在于字符串中。对不起代码 - 也生锈了,但我想你可以看到我在说什么......
Dim strPostCode As String = "HS1,HS2,HS3,HS4,HS5,HS6,HS7,HS8,HS9,IV41,IV42"
Dim SearchString As String = "HS24AB"
Dim Match As Boolean = False
Dim i As Integer
For(i = 0; i< SearchString.Length; i++)
If(strPostCode.Contains(SearchString.charAt(i)) Then
Match = True
Else
Match = False
End If
Next