Search code examples
asp-classicpostal-code

Does the part of my string exist in another string?


Sorry for this question, I'm quite rusty on my classic ASP and can't get my head around it.

I have a variable containing the start of a series of postcodes:

strPostCode = "HS1,HS2,HS3,HS4,HS5,HS6,HS7,HS8,HS9,IV41,IV42" etc etc

Now I need to see if the post code a user has entered exists in that string.

so, "HS2 4AB", "HS2", "HS24AB" all need to return a match.

Any ideas?

Thanks


Solution

  • You need to split the post codes by comma then go one by one and look for a match.

    Code would look like this:

    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
    

    The above will look for each post code anywhere in the user input e.g. "4AB HS2" will also return a match; if you want the post code to only appear in the beginning of the input, use the alternative If line that is remarked in the above code.