我的字符串的部分是否存在于另一个字符串中?

问题描述:

对不起,对于这个问题,我对我的经典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行。

+0

我去了注释掉的另一条线,表示感谢! – lunchboxbill 2013-02-20 21:27:00

+0

欢呼,很高兴我能帮到你! – 2013-02-20 21:40:35

你可以,如果该字符的字符串中是否存在搜索字符串分割成字符,然后在一个循环检查。很抱歉的代码 - 生锈过,但我想你可以看到我在说什么......

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