Domanda

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

È stato utile?

Soluzione

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.

Altri suggerimenti

You could split your search string into characters and then in a loop check if that character exists in the string. Sorry about the code - rusty too, but I think you can see what I'm saying...

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top