Hey,
How can I check whether a character is alphanumeric??
I know I can check the ASC value and compare it, but are there any other ways?
Thanks,
Printable View
Hey,
How can I check whether a character is alphanumeric??
I know I can check the ASC value and compare it, but are there any other ways?
Thanks,
VB Code:
Public Function IAlphaNum(ByVal pstrChar As String) As Boolean Select Case Asc(pstrCar) Case vbKey0 To vbKey9 IsAlphaNum = True Case vbKeyA To vbKeyZ IsAlphaNum = True End Select End function
Woka
You can combine the two Cases. I also fixed a couple of typos and added the UCase so that it would work for lowercase as well as uppercase letters.
VB Code:
Public Function IsAlphaNum(ByVal pstrChar As String) As Boolean Select Case Asc(UCase(pstrChar)) Case vbKey0 To vbKey9, vbKeyA To vbKeyZ IsAlphaNum = True End Select End Function
IF you need to repeat this function a lot you can try something like this which is usually faster then SELECT CASE:
-- note I did this from memory, I believe the const declarations are correct. AND OR operations are faster than other compare operations.Code:Const IS_DIG as Byte = 2
Const IS_UPP as Byte =4
Const IS_LOW as Byte = 8
Function isalnum(c as Byte) as Byte
isalnum= ( c + 1) AND ( IS_DIG OR IS_UPP OR IS_LOW)
End Function
That would sure call for some coments in the code! Can you explain how it works?